# Generic, non web based templating engine?

**URL:** <https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318>\
**Category:** Ruby\
**Created:** [November 18, 2005, 8:53pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318 "2005-11-18T20:53:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.campbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@michael.campbell](https://www.ruby-forum.com/u/michael.campbell)\
**Post date:** [November 18, 2005, 8:53pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318/1 "2005-11-18T20:53:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michael.campbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@michael.campbell](https://www.ruby-forum.com/u/michael.campbell)\
**Post date:** [November 18, 2005, 8:56pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318/2 "2005-11-18T20:56:57Z")

</div>

On 11/18/05, Michael C. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![michael.campbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@michael.campbell](https://www.ruby-forum.com/u/michael.campbell)\
**Post date:** [November 18, 2005, 8:59pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318/3 "2005-11-18T20:59:58Z")

</div>

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. 🙂

James Edward G. II

---

<div class="post-metadata">

**Author:** ![michael.campbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@michael.campbell](https://www.ruby-forum.com/u/michael.campbell)\
**Post date:** [November 18, 2005, 10:48pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318/4 "2005-11-18T22:48:40Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![michael.campbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@michael.campbell](https://www.ruby-forum.com/u/michael.campbell)\
**Post date:** [November 18, 2005, 11:03pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318/5 "2005-11-18T23:03:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michael.campbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@michael.campbell](https://www.ruby-forum.com/u/michael.campbell)\
**Post date:** [November 19, 2005, 5:22pm UTC](https://www.ruby-forum.com/t/generic-non-web-based-templating-engine/48318/6 "2005-11-19T17:22:41Z")

</div>

On 11/18/05, Michael C. [removed\_email\_address@domain.invalid](mailto: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? 😉

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
