[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ERB question, not seeing variables..

warhero

7/21/2007 12:49:00 AM

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

--
Posted via http://www.ruby-....

2 Answers

Jesse Brown

7/21/2007 1:33:00 AM

0

> e = ERB.new(template_content, 0, "%<>")
> e.result

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.

--
Jesse Brown
www.cs.drexel.edu/~jrb562
not_in_units = [unit for unit in UNITS if unit not in units]

warhero

7/21/2007 1:53:00 AM

0

Jesse Brown wrote:
>> e = ERB.new(template_content, 0, "%<>")
>> e.result
>
> 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

--
Posted via http://www.ruby-....