Question

I am looking for a simple templating solution for ruby, i.e. something that will let me write php-like or jsp-like templates (html with embedded code tags), but in ruby. jsp is no good for me, because most hosting services (I'm on dreamhost) don't support it on their default package, and it's a little cumbersome to set up. php is easy to get up and running, but I hate the syntax, and I miss all the greatness of ruby.

erb seems to be close to what I'm looking for, but how do I set up the server (htaccess file etc) to serve it?

P.S. Please do not suggest Ruby On Rails. It's a huge overkill for what I'm doing.

Was it helpful?

Solution

You can find pretty neat tutorial about how to set up all the server stuff for serving html with eruby over here.

OTHER TIPS

ERB is built into the language as part of its core. In case you don't know what it is, it's the template engine behind Ruby on Rails that comes with Ruby. (So don't attribute it to Rails.) It is powerful enough for any purpose you have in mind.

I have used ERB on my own to make a script that took an RSS feed and output the articles into an html file. So don't complain that it is heavyweight ... because it was slower to download the articles and parse them through the XML parser than it was to output the html.

if your webserver has rack you should check out waves. It's a minimal framework that will let you do erb or erubis templated with minimal overhead.

I would recommend Sinatra.rb with erb or haml. for small sites hosting you can use heroku for free.

To add something to the first answer, if you have performance issues with ERB, then you can try Erubis. It is a very fast and compatible version of ERB.

ok, I get it. Thanks.

ERB is the way to go for me. The configuration script for dreamhost is here, if you're interested:

ERB is the obvious choice, but I've grown to like Haml & Sass too. They're extremely terse.

If what you're looking for is a microframework, Sinatra is nice and easy, and supports multiple templating languages, including ERB and Haml.

Using template engines require you to write HTML code in an outer file, and if you are concerned that introducing a framework if overkill, then so is erb. It doesn't work as part of the program execution, but you need to run that against your erb file. You might want to look into the dom gem that I have developed. Using this gem, you can generate HTML strings directly within Ruby script like this:

require "dom"
["foo".dom(:span, class: "bold"), "bar"].dom(:div).dom(:body).dom(:html)
# => "<html><body><div><span class=\"bold\">foo</span>bar</div></body></html>"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top