Thanks Clemens,
Yes, you've got the problem right. I may to resort to
something like what you propose. The reason I wanted to
avoid such a thing is that eval is expensive, so
my idea was to do the expensive eval just once, but
we can pass different variables to the newly created
method many many times, sometimes we would not even
pass in all the variables referenced in the template
in which case the desired result would be for the template
to print out everything and just print nothing where
the non-existant variables were referenced in the
template.
This of course brings up the fact that I need to blow
away all instance variables in the JotsOutput class
between each time the template is rendered with new
data.
In perl I did this by keeping track of the variables
that were passed in for each rendering in a hash (called
%_varnames) , and then before we sent in new data we did:
# blow away symtab entries:
foreach my $var (keys %$OUTPUT_PKG::_varnames) { undef *$var; }
What I think I will end up doing is detecting all the
variables referenced in the template and make sure
they are at least defined and set to nil before rendering.
There doesn't seem to be a better way around it.
regards,
-joe
> -----Original Message-----
> From: nosuzuki / e-mail.ne.jp [mailto:nosuzuki / e-mail.ne.jp]On Behalf Of
> Clemens Wyss
> Sent: Thursday, December 14, 2000 10:40 PM
> To: ruby-talk ML
> Subject: [ruby-talk:7292] Re: Template system in Ruby (NameError)
>
>
> Joseph,
> I would suggest to (pre-)initialize the placeholders detected in the file
> through eval.
> i.e. after having opened and read-in the file:
> ...
> placeholders = file_content.scan(/\#\{(.*)\}/)
> placeholders.each do placeholder
> eval(placeholder.to_s + " = 'missing'")
> end
> ...
>
> Hope I got your problem right?
> Also, I have not verified the regexp!
>
> Clemens
>
>