Class: Merb::Template::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-builder/template.rb

Defined Under Namespace

Modules: Mixin

Class Method Summary collapse

Class Method Details

.compile_template(io, name, locals, mod) ⇒ Object

Defines a method for calling a specific Builder template.

Parameters

path<String>

Path to the template file.

name<~to_s>

The name of the template method.

locals<Array>

A list of locals to assign from the args passed into the compiled template.

mod<Class, Module>

The class or module wherein this method should be defined.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/merb-builder/template.rb', line 13

def self.compile_template(io, name, locals, mod)
  path = File.expand_path(io.path)
  method = mod.is_a?(Module) ? :module_eval : :instance_eval
  assigns = locals.inject([]) do |a, l|
    a << "#{l} = _locals[#{l.inspect}];"
  end.join
  mod.send(method, %{
    def #{name}(_locals={})
      @_engine = 'builder'
      
      #{assigns}
      
      config = (Merb.config[:builder] || {}).inject({}) do |c, (k, v)|
        c[k.to_sym] = v
        c
      end
      xml = ::Builder::XmlMarkup.new(config)
      self.instance_eval %{#{io.read}}
      xml.target!
    end
  })
  
  name    
end