Class: Malt::Engine::Tenjin

Inherits:
Abstract show all
Defined in:
lib/malt/engines/tenjin.rb

Overview

Tenjin

Let to it’s own designs, Tenjin renders data as template instance variables. But that will not work for Malt, so use regular variables instead.

Instance Attribute Summary

Attributes inherited from Abstract

#settings

Instance Method Summary collapse

Methods inherited from Abstract

#cache?, default, #initialize, register, type

Constructor Details

This class inherits a constructor from Malt::Engine::Abstract

Instance Method Details

#create_engine(params = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/malt/engines/tenjin.rb', line 68

def create_engine(params={})
  file, text = parameters(params, :file, :text)

  opts = engine_options(params)

  opts[:escapefunc] ||= 'CGI.escapeHTML'

  cached(text, file, opts) do
    ::Tenjin::Template.new(nil, opts)
  end
end

#prepare_engine(params = {}, &content) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/malt/engines/tenjin.rb', line 40

def prepare_engine(params={}, &content)
  text, file = parameters(params, :text, :file)

  file ||= "(tenjin)"

  engine = create_engine(params)
  script = engine.convert(text, file)

  lambda do |scope, locals|
    vars, vals = [], []
    locals.each do |k,v|
      vars << k
      vals << v
    end

    code = %{
      lambda do |#{vars.join(',')}|
        _buf = ''
        #{script}
        _buf
      end
    }

    eval(code, scope.to_binding, file, 2).call(*vals)
  end
end

#render(params, &content) ⇒ Object

Render Tenjin.

Parameters:

  • params (Hash)

    a customizable set of options

Options Hash (params):

  • :escapefunc (String)

    Defaults to ‘CGI.escapeHTML’.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/malt/engines/tenjin.rb', line 21

def render(params, &content)
  into, text, file, type, scope, locals = parameters(params, :to, :text, :file, :type, :scope, :locals)

  into ||= :html

  if type == :rbhtml && into != :html
    super(params, &content) 
  else
    scope, locals = make_ready(scope, locals, &content)

    engine = prepare_engine(params)

    engine.call(scope, locals)
  end
end