Generic, non web based templating engine?


#1

Is there a generic templating engine for ruby, a la Freemarker for
Java? What I’m looking for is something that can take a ruby “model”
(object graph, hashes, lists, etc.), a template, merge the 2 and spit
out the result.

I’d like this NOT specifically for Web usage; that is, it doesn’t
take .rhtml, or any html necessarily, doesn’t need to be run as part
of mod_ruby inside apache, etc., and ideally would be pure ruby.

I like the erb syntax, and would love to have the full power of ruby
inside the template, if possible. Something like OGNL is for java…

A quick glance at rubyforge and RAA didn’t yield much, but I’m
probably missing something.

Or, failing that, a reason I’m obviously overlooking as to why
something like this isn’t necessary and what to do instead.

Thanks


#2

On 11/18/05, Michael C. removed_email_address@domain.invalid wrote:

Is there a generic templating engine for ruby, a la Freemarker for
Java? What I’m looking for is something that can take a ruby “model”
(object graph, hashes, lists, etc.), a template, merge the 2 and spit
out the result.

Look at the work in Ruwiki’s templating engine. It’s based on the RDoc
templating engine. Both work well with web pages but are not
restricted to web pages. The Ruwiki modifications to that engine are
perhaps more appropriate to non-webpage items because I did some work
to make sure that single-line replacements work very well.

It should be extractable, but I haven’t taken the time to do so as a
separate download.

-austin


#3

On Nov 18, 2005, at 1:52 PM, Michael C. wrote:

I like the erb syntax, and would love to have the full power of ruby
inside the template, if possible.

ERb is a standard Ruby library and can be used for anything (not just
HTML), so if that’s what you want, you can have it. :slight_smile:

James Edward G. II


#4

Michael C. wrote:

Is there a generic templating engine for ruby, a la Freemarker for
Java? What I’m looking for is something that can take a ruby “model”
(object graph, hashes, lists, etc.), a template, merge the 2 and spit
out the result.

Perhaps PageTemplate:
http://rubyforge.org/projects/pagetemplate/

mature; stricter separation of code and template in that it doesn’t
involve embedding any ruby code in templates and doesn’t even allow any
parameter passing to loop functions. good if that’s the sort of
templating you want.

alex


#5

On Friday 18 November 2005 12:52 pm, Michael C. wrote:

inside the template, if possible. Something like OGNL is for java…
You can just use erb. It doesn’t have to be used for web things, if you
like
the syntax.

There are a bunch of other plain old templating packages out there.
Given
what you describe, maybe xtemplate or amrita2 might interest you?
Kwartz is
interesting.

I also use IOWA for generic content generation. On demand reports, CSV
files
for import into Excel, and customized email generation, primarily, but
that’s
probably not what you want.

Kirk H.


#6

On 11/18/05, Michael C. removed_email_address@domain.invalid wrote:

I like the erb syntax, and would love to have the full power of ruby
inside the template, if possible. Something like OGNL is for java…

If you like erb, why not use it? :wink:

Here’s an example to get you started:

require ‘erb’

def do_erb(str, erb_binding, trim_mode = ‘%’)
str.untaint
erb = ERB.new(str, $SAFE, trim_mode)
erb.result(erb_binding)
end

class Foo
attr_accessor :greeting
end

foo = Foo.new
foo.greeting = “Hello”
template = <<EOT
<%= greeting %> from simple erb template
% 1.upto(10) do |i|
item <%= i %>
% end
EOT

puts do_erb(template, foo.instance_eval{binding})

— OUTPUT —
Hello from simple erb template
item 1
item 2
item 3

Regards,

Sean