Let’s first start with a code snippet to explain the issue:
= Haml::Engine.new('#bar= yield').render do
this should show up inside div#bar, right?
Given that code and according to the haml::engine docs and more than one stackoverflow post, I would expect that the string “this should show up inside of the div, right?” would in fact end up inside the div#bar element, resulting in some html looking like this:
<div id="bar">
this should show up inside of the div, right?
</div>
However, this is what I actually get:
this should show up inside of the div, right?
<div id="bar">
0
</div>
So, two questions:
- Why does the content from the block show up outside div#bar and
- What is that 0 inside the div#bar element?
This is the correct behavior of code your wrote. Let see for example:
%div= vardoesn’t return as a result of block execution. It goes right in total html.In your case:
string
this should show up inside div, right?renders as haml and goes directly into html to, and you block returns 0. That’s why we see such result:So, in your case you should do like that: