I'm trying to put together a templating system in ruby.
The basic idea is to eval a chunk of text after parsing
it and looking for directives (i.e. looping constructs,
includes, etc...) I only want to eval the text once
but will change the data sent into it, and display the
template many times. something like this:
t1 = Jots.new(filename) # just parse and eval once... creates a method
t2 = Jots.new(filename) # just parse and eval once... creates a method
while(in_accept_loop)
t1.setvars("varname1" => var1, "varname2" => var2)
print t1.execute();
t2.setvars("varname1" => var1, "varname2" => var2)
print t2.execute();
end
I have run into a couple of problems. First there is the
"NameError" exception problem.... If the template looks like
this:
Hello #{first_name} #{last_name},
Thank you for clicking on me.
and gets parsed into:
def output
str = %Q(
Hello #{@first_name} #{@last_name},
Thank you for clicking on me.)
return str
end
and either @first_name or @last_name are not defined, then
it bombs out. What I would like it to do is print out
the string just replacing the missing variable with nothing,
but leave the rest of the string intact. How can I accomplish
that? I tried catching the exception, which I can do, but
it doesn't print out the string...
I'll formulate the other problem in a seperate email.
thanks,
-joe