Class: Ramaze::Template::Tenjin

Inherits:
Template show all
Defined in:
lib/ramaze/template/tenjin.rb

Overview

Is responsible for compiling a template using the Tenjin templating engine. Can be found at: www.kuwata-lab.com/tenjin/

Instance variables in your action method are automatically available in the template:

class MainController < Ramaze::Controller
  engine :Tenjin
  def index
    @names = ['Alice', 'Bob', 'Charlie']
    @greeting = 'Hello'
    %q{
    <html><body>
      <h1><?rb for name in @names ?><p>#{@greeting} #{name}</p><?rb end ?></h1>
    </body></html>
    }
  end
end

Note that only instance vars are available and methods (including helper methods) are not available in the template. A few common helpers are made generally available though by virtue of the modifications to Tenjin::Context at the top of this file.

Class Method Summary collapse

Methods inherited from Template

caching_compile, reaction_or_file, render_method, result_and_file, wrap_compile

Methods included from Helper::Methods

extend_object, #helper, included

Class Method Details

.compile(action, template) ⇒ Object

Compile a template, returning an instance of ::Tenjin::Template.



66
67
68
69
70
# File 'lib/ramaze/template/tenjin.rb', line 66

def compile(action, template)
  tenjin = ::Tenjin::Template.new
  tenjin.convert(template)
  return tenjin
end

.context(action) ⇒ Object

Return a hash of all instance variables from the action. For each instance var @foo, we put :foo => @foo in the hash.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ramaze/template/tenjin.rb', line 53

def context action
  hash = {}
  action_binding = action.binding
  var_names = action.instance.instance_variables
  var_names.each do |var_name|
    # Strip the @ from the front of the var name to use as hash key,
    # then simply use the value of the instance var as the hash value.
    hash[var_name[1..-1]] = eval(var_name, action_binding)
  end
  hash
end

.transform(action) ⇒ Object

Transforms an action into the XHTML code for parsing and returns the result.



45
46
47
48
49
# File 'lib/ramaze/template/tenjin.rb', line 45

def transform(action)
  tenjin = wrap_compile(action)
  # Tenjin can't deal with a Binding so we construct a hash for it.
  tenjin.render(context(action))
end