Class: Redminerb::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/redminerb/template.rb

Overview

Class to render Redminerb’s ERB templates

Constant Summary collapse

TEMPLATES_DIR =
'.redminerb/templates'

Class Method Summary collapse

Class Method Details

.render(name, resource, options = {}) ⇒ Object

Renders the template name using resource assigned to a local variable with that same name in its binding (i.e. the template will have a local variable with the same name of the template that let us access to the resource).

Parameters

  • name - Filename (without the erb extension) of the template in the templates directory.

  • resource - The object that will be available into the template with the same name as the the template itself.

Example:

# With this content in templates/issue.erb:

Title: <%= issue[:subject] %>

# ...we could call +render+ this way:

Redminerb::Template.render(:issue, {subject: 'Fixme!'})


34
35
36
37
38
39
# File 'lib/redminerb/template.rb', line 34

def render(name, resource, options = {})
  b = binding
  b.local_variable_set(name, resource)
  template = _read_template(options['template'] || name)
  ERB.new(template).result(b)
end