class Haml::Engine
def def_method(object, name, *local_names)
It extends Haml::Helpers, and various instance variables are set
(either the scope object or the "self" object of the scope binding).
Note that Haml modifies the evaluation context
obj.render(:foo => "Hello!") #=> NameError: undefined local variable or method `foo'
Haml::Engine.new("%p= foo").def_method(obj, :render)
obj = Object.new
# This doesn't
obj.render(:foo => "Hello!") #=> "
Hello!
"Haml::Engine.new("%p= foo").def_method(obj, :render, :foo)
obj = Object.new
# This works
For example:
This is done with the +local_names+ argument.
the local variables which can be assigned must be pre-declared.
However, due to an unfortunate Ruby quirk,
The first argument of the defined method is a hash of local variable names to values.
"foobar".upcased_div #=> "
FOOBAR
\n"Haml::Engine.new(".upcased= upcase").def_method(String, :upcased_div)
t.render #=> "
\n Today's date is\n
Fri Nov 23 18:28:29 -0800 2007
\n\n"Haml::Engine.new("%p\n Today's date is\n .date= self.to_s").def_method(t, :render)
t = Time.now
For example:
the method will instead by defined as an instance method.
If +object+ is a class or module,
that renders the template and returns the result as a string.
with the given name
Defines a method on +object+
def def_method(object, name, *local_names) method = object.is_a?(Module) ? :module_eval : :instance_eval object.send(method, "def #{name}(_haml_locals = {}); #{precompiled_with_ambles(local_names)}; end", @options[:filename], @options[:line]) end