Forum: Ruby generic, non web based templating engine?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
577053972bc30f59cf29714765a8b49f?d=identicon&s=25 michael.campbell (Guest)
on 2005-11-18 20:53
(Received via mailing list)
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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-18 20:56
(Received via mailing list)
On 11/18/05, Michael Campbell <michael.campbell@gmail.com> 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 james (Guest)
on 2005-11-18 20:59
(Received via mailing list)
On Nov 18, 2005, at 1:52 PM, Michael Campbell 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.  :)

James Edward Gray II
669b7046f02e5dfc4bda4421f1069731?d=identicon&s=25 alex (Guest)
on 2005-11-18 22:48
(Received via mailing list)
Michael Campbell 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
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 khaines (Guest)
on 2005-11-18 23:03
(Received via mailing list)
On Friday 18 November 2005 12:52 pm, Michael Campbell 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 Haines
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2005-11-19 17:22
(Received via mailing list)
On 11/18/05, Michael Campbell <michael.campbell@gmail.com> 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? ;)

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
This topic is locked and can not be replied to.