class Malt::Engine::Erector

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

@see erector.rubyforge.org/userguide.html

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.

Public Instance Methods

create_engine(params={}) click to toggle source

Erector constructor support caching.

# 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) click to toggle source

Return Erector parser, ready to render results.

# 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) click to toggle source
Calls superclass method Malt::Engine::Abstract#render
# 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

Private Instance Methods

require_engine() click to toggle source

Load Erector library if not already loaded.

# File lib/malt/engines/erector.rb, line 83
def require_engine
  return if defined? ::Erector
  require_library 'erector'
end