Class: Zero::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/zero/renderer.rb

Overview

the base renderer for getting render containers

This class handles templates and render coontainers, which can be used for the actual rendering.

To use this renderer you have to give it a template path and optionally a map of shorthand type descriptions to fully types. This will then be used to extend the internal map of templates to possible formats in a way, that you will be able to answer xhtml and html requests with the same template.

When the object is initialized and you are sure, everything is loaded, call #read_template_path! and the template tree will be built. Without this step, you will probably don’t get any output.

After the setup, the renderer can be used to build render containers, which then can be used to actually render something.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path, type_map = {}) ⇒ Renderer

initializes a new Renderer

This method takes a path to the base template directory and a type map. This type map is used to extend the possible renderings for different types, which the clients sends.

Examples:

create a simple renderer

Renderer.new('app/templates')

create a renderer with a small map

Renderer.new('app', {
  'html' => ['text/html', 'application/html+xml'],
  'json' => ['application/json', 'application/aweomse+json']
})

Parameters:

  • template_path (String)

    a string to templates

  • type_map (Hash) (defaults to: {})

    a map of simple types to complex ones



36
37
38
39
# File 'lib/zero/renderer.rb', line 36

def initialize(template_path, type_map = {})
  @template_path = template_path + '/'
  @type_map = type_map
end

Instance Attribute Details

#template_pathString (readonly)

get the path to the templates

Returns:

  • (String)

    the base template path



46
47
48
# File 'lib/zero/renderer.rb', line 46

def template_path
  @template_path
end

#templatesHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

get the tree of templates

Returns:

  • (Hash)

    the template tree



50
51
52
# File 'lib/zero/renderer.rb', line 50

def templates
  @templates
end

#type_mapHash (readonly)

returns the hash of type conversions

Returns:

  • (Hash)

    type conversion



43
44
45
# File 'lib/zero/renderer.rb', line 43

def type_map
  @type_map
end

Instance Method Details

#read_template_path!Self

load the template tree

This method gets all templates in the ‘template_path` and builds an internal tree structure, where templates and types direct the request to the wanted template.

Returns:

  • (Self)

    returns the object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zero/renderer.rb', line 58

def read_template_path!
  # TODO clean up later
  @templates = {}
  search_files.each do |file|
    parts = file.gsub(/#{template_path}/, '').split('.')
    @templates[parts[0]] ||= {}

    # Set default value
    types = 'default'
    # Overwrite default value, if it's set in template path
    if parts.count > 2 then
      types = parts[1]
    end

    read_type(types).each do |type|
      @templates[parts[0]][type] = file
    end
  end
end

#render(name, type, context) ⇒ String

render a template

This method will render the given template, based on the type in the given context.

Parameters:

  • name (String)

    the name of the template

  • type (Array)

    a list of accept types used to find the template

  • context (Object)

    the context in which to evaluate the template

Returns:

  • (String)

    the rendered content



86
87
88
# File 'lib/zero/renderer.rb', line 86

def render(name, type, context)
  template(name, type).render(context)
end