Class: Malt::Engine::Erector

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

Overview

The Erector template engine handles a builder-style template format.

For Erector templates the data is passed in as attribute variables.

A simple example template:

div do
  h1 @name
  div @state, :id=>'state'
  div @content, :id=>'yield'
end

IMPORTANT! Erecotor templates do not currently support scope.

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

Erector constructor support caching.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/malt/engines/erector.rb', line 62

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

  cached(prefix, text) do
    Class.new(::Erector::Widget) do
      module_eval %{
        def #{prefix}; self; end
      } if prefix

      module_eval %{
        def content
          #{text}
        end
      }
    end
  end
end

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

Return Erector parser, ready to render results.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/malt/engines/erector.rb', line 39

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

  scope, locals = make_external(scope, locals, &content)

  unless scope.respond_to?(:to_struct)
    scope_locals = {}
    scope.instance_variables.each do |k|
      next if k == "@target"
      name = k.to_s.sub('@','').to_sym
      v = scope.instance_variable_get(k)
      scope_locals[name] = v
    end
    locals = scope_locals.merge(locals)
  end

  create_engine(params).new(locals)
end

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/malt/engines/erector.rb', line 27

def render(params={}, &content)
  into = params[:to] || :html

  case into
  when :html, :xhtml
    prepare_engine(params, &content).to_html
  else
    super(params, &content)
  end
end