Include html from haml

I am familiarized with several haml tutorials, but I cannot figure out how to include the html file (directly) in a haml document. For instance:

a.haml

.header hello = render :partial => "b.html" 

b.html

 world 

Expected Result:

 <div class='header'> hello world </div> 

I tried = render "b.html" . 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>' 

It looks like I need to enable the library in order to use the โ€œvisualizationโ€ or install the library. How to dump unformatted b.html text into a document where I want?

+6
source share
1 answer

render is a method from Rails (and some other frameworks) and is not available in pure Haml. To directly include the contents of another file, you can simply read the file in your Haml:

 .header hello = File.read "b.html" 

which gives the expected result in this case.

This is a simple inclusion of the contents of the file directly into the output. If you want another file to be processed in some way, you will need to do it yourself, for example. if you want to display another Haml file, you can do something like this:

 .header Some text = Haml::Engine.new(File.read("a_file.haml")).render 

If you do this with different template libraries, you can take a look at Tilt .

These examples are very simple, and you really should use them for something like a web application - theyre only for creating static files. If you are learning Haml for use in developing web applications, then helpers from any structure you use will still be available, and you should use them, for example. render in Rails; haml , erb , markdown , etc. in Sinatra.

+11
source

All Articles