13. September, 2007
13. September, 2007
in by Michael Neumann

For my blogging software I am using Tenjin as template engine. I also tried Kwartz which is also very nice. With Tenjin you embed Ruby into HTML code, in the same way as you’d do in Erb or PHP. This turns out to get pretty ugly. Kwartz completely separates HTML from presentation logic. It is a nice concept, but the presentation logic file can get quite complex for just simple things.

So why not embed HTML in Ruby?

# Example for embedding HTML in Ruby :)

class MyView

  def render(posts)
    for post in posts
      render_post(post)
    end
  end

  def render_post(post)
    #<div id="${post.id}">
    if post.abstract?
      #<p>${post.abstract}</p>
    else
      #<p>${post.body}</p>
    end
    #</div>
  end

end

I think you got the idea, right? I just misuse comments to embed HTML in Ruby. This is very easy to parse and looks quite nice in an editor and it should be easy to tell vim to colorize the embedded HTML correctly. Note that everything that starts with “#<” would be embedded HTML so that you’d still be able to use regular Ruby comments.