开发者

Rails + Haml: How to unescape tag parameter?

I'm trying to accomplish the following content in source code:

<div id="box<%=id%>"></div>

Without escaping any signs in Haml.

%div{ :id => "box_<%=id%>" }

produces

<div id='box_&lt;%=id%&gt;'></div>

Right now the only way I can do th开发者_如何学JAVAis with Haml is to use :plain filter and hardcode HTML without using any View Helpers. How can I fix that?

I need this because I'm forced to follow this convention because of third-party syntax convention: JavaScript Micro-Templating

Reference:

Haml reference


You say you're coding in Haml, but the brackets indicate Erb.

  • Step 1. Try normal Ruby interpolation:

    %div{ :id => "box_#{id}" }
    
  • Step 2. There is no step 2.

Update: When I wrote this answer, I had not used a JS templating framework before. My apologies for not grokking why this was necessary.


Basically, just don't use HAML for that one statement. For example, assuming you are doing something like this in HAML right now:

%script#template(type="text/html")
    %div{ :id => "box_<%=id%>" }

Instead, do:

%script#template(type="text/html")
    <div id="box_<%=id%>"></div>

It will interpret the script tag as HAML correctly, then since you aren't using any %, #, etc. to start the next line it will interpret that line as plain text and pass it through. Seems to work fine for me and I have a very similar case to yours. The one thing I did do was change the jQote (the templating engine I'm using for my Javascript templating) tag from % to $ with this Javascript statement in the head of the page:

%script( type="text/javascript")
    $(function() { $.jqotetag( '$' ); });

That way the % signs don't confuse the Ruby interpreter. So the final example would be:

%script#template(type="text/html")
    <div id="box_<$=id$>"></div>

Good luck!


http://haml.info/docs/yardoc/file.REFERENCE.html#unescaping_html

! %div(id="box_<%= data.id %>")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜