ERB question, not seeing variables..

A

Aaron Smith

I'm trying to do some template parsing in a class.. here is my class:

require 'erb'
class TemplateParser
def TemplateParser.parse(template, params)
params.each do |k,v|
self.instance_variable_set:)"@#{k}",v)
end
template_content = ''
File.open(template, "r") do |f|
while line = f.gets
template_content << line
end
end
e = ERB.new(template_content, 0, "%<>")
e.result
end
end

so in my template.. if I have a variable called "revisions". and it is
set in the params[:revisions] hash. It defines an instance on the class
called revisions, but ERB is not seeing that.. What can I do get around
this?

Thanks
 
J

Jesse Brown

e = ERB.new(template_content said:

I'm not sure if it will be helpful in your situation, but e.result
will accept a binding parameter. That binding is used to resolve local
variables in the template.
 
A

Aaron Smith

Jesse said:
I'm not sure if it will be helpful in your situation, but e.result
will accept a binding parameter. That binding is used to resolve local
variables in the template.

yeah I got if figured out.. thanks..

require 'erb'
class TemplateParser
def TemplateParser.parse(template, params)
params.each do |k,v|
self.instance_variable_set:)"@#{k}",v)
end
template_content = ''
File.open(template, "r") do |f|
while line = f.gets
template_content << line
end
end
e = ERB.new(template_content, 0, "%<>")
e.result(self.send("binding"))
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top