J
Jeremy Wells
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:
Template:
<xml><%=@variable1%> <%=variable2%></xml>
class:
class Renderer
def initialize(template_file, apply_binding)
@file = template_file
@binding = apply_binding
end
def render
@variable2 = "There"
template = ERB.new(File.read(@file))
template_results = template.result(@binding)
# Perform on template results
end
end
Program:
@variable1 = "Hello"
renderer = Renderer.new('template.xml', binding)
renderer.render
current output: <xml>Hello</xml>
desired output: <xml>Hello There</xml>
So I want the renderer class to add @variable2 to the binding before
calling the template.
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:
Template:
<xml><%=@variable1%> <%=variable2%></xml>
class:
class Renderer
def initialize(template_file, apply_binding)
@file = template_file
@binding = apply_binding
end
def render
@variable2 = "There"
template = ERB.new(File.read(@file))
template_results = template.result(@binding)
# Perform on template results
end
end
Program:
@variable1 = "Hello"
renderer = Renderer.new('template.xml', binding)
renderer.render
current output: <xml>Hello</xml>
desired output: <xml>Hello There</xml>
So I want the renderer class to add @variable2 to the binding before
calling the template.