Class: Zero::Renderer

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

Overview

This class helps with rendering of content.

The purpose of this class is to render templates. All variables pushed into the renderer should be already processed, so that the raw data can be used.

The workflow of this class is like the following.

  • setup the type mapping

  • create a new instance of the class to prepare rendering

  • call #render to process the template

The call to #render will return the String representation of the template with all data given.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Renderer) initialize(path, context, accept_types)

take the path and render the template within the context

Parameters:

  • path (String)

    the relative path to the template

  • context (Object)

    the object to process on

  • accept_types


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

def initialize(path, context, accept_types)
  accept_types ||= Request::Accept.new('text/html')
  @path    = find_template(path, accept_types)
  @context = context
end

Class Method Details

+ (Object) template_path=(path)

set a base path for template search

Parameters:

  • path (String)

    the path to the template base dir



20
21
22
# File 'lib/zero/renderer.rb', line 20

def template_path=(path)
  @@path = path + '/'
end

+ (Hash) type_map

returns the type map

Returns:

  • (Hash)

    the mapping for types



40
41
42
# File 'lib/zero/renderer.rb', line 40

def type_map
  @@map ||= {}
end

+ (Object) type_map=(map)

save a mapping hash for the type

With that it is possible to map long and complex contant types to simpler representations. These get then used in the finding process for the best fitting template.

Examples:

Zero::Renderer.map = {'text/html' => 'html'}

Parameters:

  • map (Hash)

    maps the content type to a simple representation



34
35
36
# File 'lib/zero/renderer.rb', line 34

def type_map=(map)
  @@map = map
end

Instance Method Details

- (String) render

render the template within the context

Returns:

  • (String)

    the rendered template



57
58
59
# File 'lib/zero/renderer.rb', line 57

def render
  Tilt.new(@path).render(@context)
end