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="<%= from_class %>">
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="<%= from_class %>">
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 badges3 Answers
Reset to default 5In 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 = "&".html_safe
%a(href='/posts' data-icon=foo aria-hidden='true')