I have a class that takes a template, parses it through ERB, then takes the output for some custom manipulation. When the class is created as an object a binding is passed to it to be passed to the template. Is it possible to get the class to insert a variable into the binding? Example:
def evaluate_locals(local_assigns = {})
b = binding
local_assigns.each { |key, value| eval "#{key} =
local_assigns[\"#{key}\"]", b }
b
end
The trick is to use eval with binding as an additional argument.
···
On 12/7/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
I have a class that takes a template, parses it through ERB, then takes
the output for some custom manipulation. When the class is created as an
object a binding is passed to it to be passed to the template. Is it
possible to get the class to insert a variable into the binding? Example:
surely this won't work as the binding in my case is coming from an external source, so the ruby in the eval won't have access to the variable in the render method?
Jan Svitok wrote:
···
On 12/7/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
I have a class that takes a template, parses it through ERB, then takes
the output for some custom manipulation. When the class is created as an
object a binding is passed to it to be passed to the template. Is it
possible to get the class to insert a variable into the binding? Example:
So once more: The trick is to use eval with binding as an additional argument.
That means when eval gets a binding as the second argument, it will operate
in that context. In this case, if you eval("@var = 123",
some_binding), the variable @var will be set in that binding.
Beware that you are modifying the original binding, so you might
corrupt the original caller.
Maybe @binding = apply_binding.dup will be neccessary.
class TestERB < Test::Unit::TestCase
def test_variable @variable1 = "Hello"
renderer = Renderer.new('template.xml', binding)
assert_equal "<xml>Hello There</xml>", renderer.render
end
end
···
On 12/8/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
surely this won't work as the binding in my case is coming from an
external source, so the ruby in the eval won't have access to the
variable in the render method?
Ah ok, I get it but here is the problem, which I didn't specify... @variable2 doesn't hold static data, it holds an object, which is created in the render method like so:
That's what I meant by the variable not being in the binding, thus not accessible to eval.
Jan Svitok wrote:
···
On 12/8/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
surely this won't work as the binding in my case is coming from an
external source, so the ruby in the eval won't have access to the
variable in the render method?
So once more: The trick is to use eval with binding as an additional argument.
That means when eval gets a binding as the second argument, it will operate
in that context. In this case, if you eval("@var = 123",
some_binding), the variable @var will be set in that binding.
Beware that you are modifying the original binding, so you might
corrupt the original caller.
Maybe @binding = apply_binding.dup will be neccessary.