Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering HAML partials from within HAML outside of Rails

Tags:

ruby

haml

I'm using HAML to generate some static html pages for a site, and I was wanting to split out common components into partials that I can include in multiple pages, just like in Rails. However I don't want to use the whole Rails stack to do this as it seems like overkill.

I've looked around on the Internet but haven't found anything, better than just doing something like:

Haml::Engine.new(IO.read("header.haml")).render

Is there a nicer way of including so-called partials from within HAML? An existing filter or command that I'm missing?

like image 205
Daemin Avatar asked Sep 06 '09 18:09

Daemin


3 Answers

It's best to combine haml & sass with a tool for building static websites. Here's some links:

  • StaticMatic
  • Serve
  • Haml Jekyll -- a fork of Jekyll that supports haml.
  • Middleman

I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.

like image 89
chriseppstein Avatar answered Nov 15 '22 21:11

chriseppstein


Darn, you're right – there isn't a built-in way. I've used helpers with the haml command line, but always ones whose output was already HTML formatted.

My best suggestion is to write a partial() method and require it. It looks like you have already started down this path. I would suggest anyone writing such a function consider keeping the original binding around in some way. Haml::Helpers#capture_hame looks to be the easiest way to make this happen.

If execution speed is an issue, it might also be a good idea to cache some of the parsed template in the same way that Merb does it.

If someone does get some code working, please put it up on GitHub and make a comment here.

like image 45
John F. Miller Avatar answered Nov 15 '22 19:11

John F. Miller


I finally found what I was looking for, so I thought I'd share my find with you. you can simply use the same syntax you use for haml in the first place - http://www.semicomplete.com/blog/geekery/partials-in-haml-in-sinatra.html

/views/file.haml

%h3 Title
%div= haml :content, :layout => false

/views/content.haml

%p your content here
like image 21
AxelTheGerman Avatar answered Nov 15 '22 20:11

AxelTheGerman