6

I'm building an application using Backbone.js, Underscore.js, HAML, and Coffeescript.

The problem I'm having is getting variables to interpolate inside of html element attributes.

<% _.each(collection.models, function(document) { %>
%tr
  %td
    %input{:type => 'checkbox', :name => "documents[]", :value => "<%= document.attributes.id %>"}
  %td <%= document.attributes.id %>
  %td <%= document.attributes.name %>

  <% } %>
<% }); %>

The object's values are displaying properly inside of the <td>, but not within the input's value attribute.

Is interpolation inside of an element's attributes possible? I was not able to find a solution.

Thanks

3
  • I'm not experienced in this area, but please have a look here if this is what you need. It supports coffeescript in attributes. Commented Nov 30, 2011 at 20:53
  • I'm not sure what's wrong with your code, but I have used interpolation with underscore templates. Commented Dec 1, 2011 at 3:40
  • The problem is with interpolation inside of html element attributes. Commented Dec 1, 2011 at 13:17

3 Answers 3

4

The solution to this problem is to use HAML's :escape_attrs option.

Haml::Engine.new(template, :escape_attrs => false).render
Sign up to request clarification or add additional context in comments.

Comments

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:

%input{:type => 'checkbox', :name => "documents[]", :value => "<%= document.attributes.id %>".html_safe}

(Tested on rails 3.0.13)

2 Comments

This isn't working on Rails 4.2.6, but I'm not sure why. I can put the same code outside an attribute value, and my < is not escaped, but inside the attribute value, it is.
What I mean is, the documentation on html_safe says .html_safe is equivalent to raw() but though it is in fact equivalent outside attribute values, when passed through the HAML template engine, it's not, inside attribute values. This is very mysterious.
0

It looks like you aren't closing the function in the template properly ( try adding <% }); %> to the end of your template).

I'm not really familiar with HAML syntax but here's a simple example on jsfiddle using plain HTML and an underscore template. As you can see you can definitely use interpolation in middle of an elements attributes.

5 Comments

Sorry, I didn't include the complete code. I'm updating the question.
Thanks for the link. I see that they are successfully interpolating within an element's attributes.
Then perhaps the problem is with your HAML syntax, since if you look at the jsfiddle i linked to you can see that it correctly interpolates inside the checkbox's value attribute.
I missed this earlier; it appears that HAML is escaping the code inside of the element's attributes. According to the HAML documentation there is a non-haml helper that runs code in a non HAML context.
Then perhaps using a different style delimiters (for example mustache style) for underscore will help here's a link to underscore's documentation describing how to change the delimiters used for interpolation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.