开发者

Subtle difference in how HAML handles render method with block in Rails templates

开发者 https://www.devze.com 2023-03-18 21:27 出处:网络
I stumbled across what looks to be an inconsistency in how HAML handles the render method in Rails. Example 1 in ERB:

I stumbled across what looks to be an inconsistency in how HAML handles the render method in Rails.

Example 1 in ERB:

# template.html.erb
This is the index template.
<%= render :layout => 'form4', :partial => 'stuff2' %>

# _layout.html.erb
<%= form_tag do %>
    <div class="something">
        <%= yield %>
    </div>
<% end %>

# _partial.html.erb
<b>meh</b>
<%= text_field_tag 'name' %>

Example 1 in HAML:

# template.html.haml
This is the index template.
=render :layout => 'form2', :partial => 'stuff1'

# _layout.html.haml
=form_tag do
    .something
        =yield

# _partial.html.haml
%b meh
=text_field_tag 'name'

As expected, both result in the same rendering (abbreviated below):

This is the index te开发者_开发知识库mplate.
<form>
    <div class="something"> 
        <b>meh</b> 
        <input id="name" name="name" type="text" /> 
    </div> 
</form> 

Now, here's where the weirdness kicks in. When I adjust the render statement to use the block syntax as below:

In ERB:

# template.html.erb
This is the index template.
<%= render :layout => 'form3' do %>
    <b>meh</b>
    <%= text_field_tag 'name' %>
<% end %>

# _layout.html.erb
<%= form_tag do %>
    <div class="something">
        <%= yield %>
    </div>
<% end %>

In HAML:

# template.html.haml
This is the index template.
=render :layout => 'form1' do
    %b meh
    =text_field_tag 'name'

# _layout.html.haml
=form_tag do
    .something
        =yield

I get the same above rendering in the ERB version, but the HAML code outputs:

This is the index template.
<b>meh</b> 
<input id="name" name="name" type="text" /> 
<form>
    <div class='something'></div> 
</form>

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 render method of their own. Could it be possible that it isn't implemented properly to accompany the same interface as rails's (erb's?) render method?

On that note, if this is truly an inconsistency in the methods interface, does it justify opening up an issue on HAML?

Just added an example app showing behavior at https://github.com/iamvery/haml-weirdness

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 Cannot modify SafeBuffer in place error in rails 3.0.9...


Yes I am having the same issue. It is also described at https://github.com/nex3/haml/issues/412
There is a issue_412 branch at nex3's github account but it is not complete. You could take a shot at fixing haml, I decided to go back to rails 3.0.7.

0

精彩评论

暂无评论...
验证码 换一张
取 消