Note that there are some explanatory texts on larger screens.

plurals
  1. POSubtle difference in how HAML handles render method with block in Rails templates
    primarykey
    data
    text
    <p>I stumbled across what looks to be an inconsistency in how HAML handles the <code>render</code> method in Rails.</p> <p><em>Example 1 in <strong>ERB</strong>:</em></p> <pre><code># template.html.erb This is the index template. &lt;%= render :layout =&gt; 'form4', :partial =&gt; 'stuff2' %&gt; # _layout.html.erb &lt;%= form_tag do %&gt; &lt;div class="something"&gt; &lt;%= yield %&gt; &lt;/div&gt; &lt;% end %&gt; # _partial.html.erb &lt;b&gt;meh&lt;/b&gt; &lt;%= text_field_tag 'name' %&gt; </code></pre> <p><em>Example 1 in <strong>HAML</strong>:</em></p> <pre><code># template.html.haml This is the index template. =render :layout =&gt; 'form2', :partial =&gt; 'stuff1' # _layout.html.haml =form_tag do .something =yield # _partial.html.haml %b meh =text_field_tag 'name' </code></pre> <p>As expected, both result in the same rendering (abbreviated below):</p> <pre><code>This is the index template. &lt;form&gt; &lt;div class="something"&gt; &lt;b&gt;meh&lt;/b&gt; &lt;input id="name" name="name" type="text" /&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p> </p> <p>Now, <strong>here's</strong> where the weirdness kicks in. When I adjust the <code>render</code> statement to use the block syntax as below:</p> <p><em>In <strong>ERB</strong>:</em></p> <pre><code># template.html.erb This is the index template. &lt;%= render :layout =&gt; 'form3' do %&gt; &lt;b&gt;meh&lt;/b&gt; &lt;%= text_field_tag 'name' %&gt; &lt;% end %&gt; # _layout.html.erb &lt;%= form_tag do %&gt; &lt;div class="something"&gt; &lt;%= yield %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p><em>In <strong>HAML</strong>:</em></p> <pre><code># template.html.haml This is the index template. =render :layout =&gt; 'form1' do %b meh =text_field_tag 'name' # _layout.html.haml =form_tag do .something =yield </code></pre> <p>I get the same above rendering in the ERB version, but the HAML code outputs:</p> <pre><code>This is the index template. &lt;b&gt;meh&lt;/b&gt; &lt;input id="name" name="name" type="text" /&gt; &lt;form&gt; &lt;div class='something'&gt;&lt;/div&gt; &lt;/form&gt; </code></pre> <p>It is as if HAML somehow isn't respecting the block that was passed to it. According to HAML's docs, they support blocks that autoclose based on indentation, so I don't suspect that is an issue. Also, in their docs, I saw a definition for a <code>render</code> method of their own. Could it be possible that it isn't implemented properly to accompany the same interface as rails's (erb's?) <code>render</code> method?</p> <p>On that note, if this is truly an inconsistency in the methods interface, does it justify opening up an issue on HAML?</p> <p>Just added an example app showing behavior at <a href="https://github.com/iamvery/haml-weirdness" rel="nofollow">https://github.com/iamvery/haml-weirdness</a></p> <p>It's also worth noting that I noticed this change when I upgraded my rails app to 3.0.9 and haml to 3.1.2. Leaving haml at 3.0.24 resulted in <code>Cannot modify SafeBuffer in place</code> error in rails 3.0.9...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload