HAML -> Backbone Template, Unescaping HTML Paramet

2019-07-04 10:24发布

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.

2条回答
放荡不羁爱自由
2楼-- · 2019-07-04 11:10

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-04 11:14

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

查看更多
登录 后发表回答