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?
It's best to combine haml & sass with a tool for building static websites. Here's some links:
I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With