01 Apr 2012

How to call haml outside sinatra or Rails?

People would think why would you want to do that. Well, in my case I just want to use haml to generate html for me in Test so that I could inject that in to Pusher to test the functionality of the feed page. I could use static html string, but if I change haml I don’t want to come back to the test and change the string again.

So, Haml actually is just a static class that you can call anywhere, but where you call inside Sinatra or Rails controller. You could just use

haml :my_view

However, if you do this outside Sinatra, you would get an exception that haml is not found or something close.

So here’s just a simple solution plus I’ve added how to call helper in that as well.

 base = Class.new do
    include Sinatra::DateTimeHelper # This is the helper that you want to include in Haml.
  _feed_partial_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "views", "_feed_partial.haml"))
  engine = Haml::Engine.new(IO.read(_feed_partial_path))
  rendered = engine.render(base, :var_to_inject => var_to_inject) # or if you don't want to use helper it could be just engine.render(Object.new, :var_to_inject => var_to_inject)
  puts rendered

That’s it nice and easy!

Til next time,
noppanit at 00:00