class Haml::Engine
puts output
output = haml_engine.render
haml_engine = Haml::Engine.new(template)
template = File.read(‘templates/really_cool_template.haml’)
For example:
new instance and calling {#render} to render the template.
It can be directly used by the user by creating a
This is the frontend for using Haml programmatically.
def compiler
def compiler @temple_engine end
def def_method(object, name, *local_names)
-
local_names
(Array
) -- The names of the locals that can be passed to the proc -
name
(String, Symbol
) -- The name of the method to define -
object
(Object, Module
) -- The object on which to define the method
def def_method(object, name, *local_names) method = object.is_a?(Module) ? :module_eval : :instance_eval object.send(method, "def #{name}(_haml_locals = {}); #{@temple_engine.precompiled_with_ambles(local_names)}; end", @options.filename, @options.line) end
def initialize(template, options = {})
-
(Haml::Error)
- if there's a Haml syntax error in the template
Parameters:
-
options
({Symbol => Object}
) -- An options hash; -
template
(String
) -- The Haml template
def initialize(template, options = {}) @options = Options.new(options) @template = check_haml_encoding(template) do |msg, line| raise Haml::Error.new(msg, line) end @temple_engine = TempleEngine.new(options) @temple_engine.compile(@template) end
def options_for_buffer
def options_for_buffer @options.for_buffer end
def render(scope = Object.new, locals = {}, &block)
-
(String)
- The rendered template
Parameters:
-
block
(#to_proc
) -- A block that can be yielded to within the template -
locals
({Symbol => Object}
) -- Local variables that will be made available -
scope
(Binding, Object
) -- The context in which the template is evaluated
def render(scope = Object.new, locals = {}, &block) parent = scope.instance_variable_defined?(:@haml_buffer) ? scope.instance_variable_get(:@haml_buffer) : nil buffer = Haml::Buffer.new(parent, @options.for_buffer) if scope.is_a?(Binding) scope_object = eval("self", scope) scope = scope_object.instance_eval{binding} if block_given? else scope_object = scope scope = scope_object.instance_eval{binding} end set_locals(locals.merge(:_hamlout => buffer, :_erbout => buffer.buffer), scope, scope_object) scope_object.extend(Haml::Helpers) scope_object.instance_variable_set(:@haml_buffer, buffer) begin eval(@temple_engine.precompiled_with_return_value, scope, @options.filename, @options.line) rescue ::SyntaxError => e raise SyntaxError, e.message end ensure # Get rid of the current buffer scope_object.instance_variable_set(:@haml_buffer, buffer.upper) if buffer end
def render_proc(scope = Object.new, *local_names)
-
(Proc)
- The proc that will run the template
Parameters:
-
local_names
(Array
) -- The names of the locals that can be passed to the proc -
scope
(Binding, Object
) -- The context in which the template is evaluated
def render_proc(scope = Object.new, *local_names) if scope.is_a?(Binding) scope_object = eval("self", scope) else scope_object = scope scope = scope_object.instance_eval{binding} end begin eval("Proc.new { |*_haml_locals| _haml_locals = _haml_locals[0] || {};" << @temple_engine.precompiled_with_ambles(local_names) << "}\n", scope, @options.filename, @options.line) rescue ::SyntaxError => e raise SyntaxError, e.message end end
def set_locals(locals, scope, scope_object)
def set_locals(locals, scope, scope_object) scope_object.instance_variable_set :@_haml_locals, locals set_locals = locals.keys.map { |k| "#{k} = @_haml_locals[#{k.inspect}]" }.join("\n") eval(set_locals, scope) end