Interpolate inside html attributes with Underscore.js

后端 未结 3 956
自闭症患者
自闭症患者 2021-01-12 16:40

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 ele

相关标签:
3条回答
  • 2021-01-12 17:13

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

    Haml::Engine.new(template, :escape_attrs => false).render
    
    0 讨论(0)
  • 2021-01-12 17:17

    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)

    0 讨论(0)
  • 2021-01-12 17:22

    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.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题