HAML -> Backbone Template, Unescaping HTML Parameters

China☆狼群 提交于 2019-12-12 14:14:03

问题


I'm using HAML to generate templates for a Backbone.js app. I need to be able to insert <%= blah %> as an html attribute a la:

%a{:href => "myresources/<% id %>"} My Resource

and have it output

<a href='myresources/<%= id %>' >My Resource</a>

in the html template. Unfortunately, HAML escapes the html parameters leaving me with

<a href='#myresources/&lt;%= id %&gt;'>My Resource</a>

According to the HAML Reference the '!' operator can be used for unescaping strings, but not within the HTML attributes.

Also, I'd use plaintext to render the anchor tag, but since the anchor tag is the root for this particular view, I lose all of the benefits of using HAML.

Any help?

Update I didn't mention, but I'm using LiveReload to actually watch my file system and run the haml compiler, and there was a setting in LiveReload to disable HTML escapes in tag attributes. < head slap > If anyone else runs into this issue outside of LiveReload, you can also set the :escape_attrs option to false when configuring your HAML setup.


回答1:


You can configure HAML to not escape tag attributes using the escape_attrs option in your HAML configuration. See HAML Options.




回答2:


You can try using html_safe which is a method on String objects. This will escape the html characters in the variable statement (< for example) and will leave the intact for underscore to evaluate at runtime:

%a{:href => "myresources/<% id %>".html_safe} My Resource

Found on answer to Interpolate inside html attributes with Underscore.js



来源:https://stackoverflow.com/questions/10404686/haml-backbone-template-unescaping-html-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!