[Nitro] transform_template -> block

Tim Larson tim at keow.org
Tue Dec 13 15:47:15 EST 2005


On Tue, Dec 13, 2005 at 03:40:30PM +0000, Tim Larson wrote:
> ...The next question is if it is better
> to make lambdas or methods...opinions?

Here is the method version:
  # Note: Do not pass a name ending in '_action' or '_template'
  # as such methods get automatically deleted on a regular basis.
  def self.temp(name, parameters, body)
    class_eval %{
      private
      def #{name}(#{parameters})
        out, @out = @out, ''
        #{Nitro::Compiler.new.transform_template(body)}
        out, @out = @out, out
        out
      end
    }
  end
#Define a template piece:
  temp 'some_name', 'param1, param2', %{
    Hello, World!
    <?r param1.times do ?>
      I was passed \#{param2}.
    <?r end ?>
  }
#Use it within another template piece:
   #{some_name 2, 'foo'}

--Tim Larson



More information about the Nitro-general mailing list