Class: Zero::Renderer
- Inherits:
-
Object
- Object
- Zero::Renderer
- 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
-
#template_path ⇒ String
readonly
get the path to the templates.
-
#templates ⇒ Hash
readonly
private
get the tree of templates.
-
#type_map ⇒ Hash
readonly
returns the hash of type conversions.
Instance Method Summary collapse
-
#initialize(template_path, type_map = {}) ⇒ Renderer
constructor
initializes a new Renderer.
-
#read_template_path! ⇒ Self
load the template tree.
-
#render(name, type, context) ⇒ String
render a template.
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.
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_path ⇒ String (readonly)
get the path to the templates
46 47 48 |
# File 'lib/zero/renderer.rb', line 46 def template_path @template_path end |
#templates ⇒ Hash (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
50 51 52 |
# File 'lib/zero/renderer.rb', line 50 def templates @templates end |
#type_map ⇒ Hash (readonly)
returns the hash of type conversions
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.
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.
86 87 88 |
# File 'lib/zero/renderer.rb', line 86 def render(name, type, context) template(name, type).render(context) end |