[Nitro] transform_template -> block
Tim Larson
tim at keow.org
Tue Dec 13 10:40:30 EST 2005
On Mon, Dec 12, 2005 at 09:18:19PM +0000, Tim Larson wrote:
> On Mon, Dec 12, 2005 at 07:47:46PM +0000, Tim Larson wrote:
> > Is there something like Nitro::Compiler.new.transform_template
> > that instead of producing a textual chunk of code produces a
> > block/lambda/whatever that can accept parameters?
Refined the solution a bit, got rid of the trouble
with a shared @out by using nil.instance_eval:
def self.template(params, str)
nil.instance_eval %{
lambda do |#{params}|
@out=''
#{Nitro::Compiler.new.transform_template(str)}
@out
end
}
end
#Define a template piece:
@@some_name = template 'param1, param2', %{
Hello, World!
<?r param1.times do ?>
I was passed \#{param2}.
<?r end ?>
}
#Use it within another template piece:
#{@@some_name.call 2, 'foo'}
It will be compiled only when the source file
is touched, and run as full speed ruby code
when it is invoked. The next question is if
it is better to make lambdas or methods...opinions?
--Tim Larson
More information about the Nitro-general
mailing list