最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - EscapeIgnore special characters in HAML attributes - Stack Overflow

programmeradmin0浏览0评论

I'm using HAML to make html templates but am having a problem in writing attributes which will be replaced with JavaScript string templating.

The line in question looks like this:

%div{:class => "<%= from_class %>"}

HAML tries to encode the <%= %> tags:

<div class="&lt;%= from_class %&gt;">

I don't want that to happen in this case... Anyone know how to do this?

I'm using HAML to make html templates but am having a problem in writing attributes which will be replaced with JavaScript string templating.

The line in question looks like this:

%div{:class => "<%= from_class %>"}

HAML tries to encode the <%= %> tags:

<div class="&lt;%= from_class %&gt;">

I don't want that to happen in this case... Anyone know how to do this?

Share Improve this question asked Oct 29, 2010 at 2:00 Ganesh ShankarGanesh Shankar 4,8648 gold badges46 silver badges57 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

In the next version of Haml (3.1), there will be an :escape_attrs option that you'll be able to set to false to prevent this. You'll also be able to pass --no-escape-attrs on the mand line. To use this right now, you can install the alpha version with gem install haml --prerelease.

As @Natalie Weizenbaum and @rchampourlier state above, put this in an initializer

config/initializers/haml.rb

Haml::Template.options[:escape_attrs] = false

Also note that because haml determines it's own order of classes:

This, because of the spaces:

.input-group-addon{class: "<%= field_name %>"}

Will render to this which doesn't work for templates:

<div class="%> <%= field_name input-group-addon">

The solution is to move the dot class (.input-group-addon) into the class: text:

%div{class: "<%= field_name %> input-group-addon"} 

Which will render what we want:

<div class="<%= field_name %> input-group-addon">

It may work for you to use no spaces (<%=field_name%>) but if you need any template logic that requires spaces, bring the .dot-class into the text...

From this answer, use a separate ruby variable with html_safe:

- foo = "&#x0026".html_safe
%a(href='/posts' data-icon=foo aria-hidden='true')
发布评论

评论列表(0)

  1. 暂无评论