I'm fresh off a couple of haml tutorials, but I can't figure out how to include an html file (directly) into a haml document. For example:
.header
hello
= render :partial => "b.html"
world
<div class='header'>
hello
world
</div>
I've tried = render "b.html"
as well. I get as an error
haml a.haml --trace
test.haml:8:in `block in render': undefined method `render' for #<Object:0x000000018b2508> (NoMethodError)
from /usr/lib/ruby/vendor_ruby/haml/engine.rb:129:in `eval'
from /usr/lib/ruby/vendor_ruby/haml/engine.rb:129:in `render'
from /usr/lib/ruby/vendor_ruby/haml/exec.rb:313:in `process_result'
from /usr/lib/ruby/vendor_ruby/haml/exec.rb:43:in `parse'
from /usr/lib/ruby/vendor_ruby/haml/exec.rb:23:in `parse!'
from /usr/bin/haml:9:in `<main>'
Which sounds like I need to include a library to use "render" or install a library. How do I dump the unformatted text of b.html
into the document where I want it?
render
is a method from Rails (and some other frameworks), and isn’t available in pure Haml. To include the contents of another file directly you could simply read the file in your Haml:
.header
hello
= File.read "b.html"
which gives the input you expect in this case.
This is simple inclusion of the file contents directly in the output though. If you want the other file to be processed in some way you will need to do it yourself, e.g. if you want to render another Haml file you could do something like this:
.header
Some text
= Haml::Engine.new(File.read("a_file.haml")).render
If you’re doing this with different template libraries you might want to look at Tilt.
These examples are very simple and you should’t really use them for something like a web app – they’re only really for generating static files. If you’re learning Haml to use in web app development then the helpers from whatever framework you’re using will still be available and you should use those, e.g. render
in Rails; haml
, erb
, markdown
etc. in Sinatra.
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