How could I escape a & in Haml so that it compiles to & instead of &? (Haml noob)

前端 未结 3 924
轻奢々
轻奢々 2020-12-20 04:09

I am trying to use the Icomoon icon font with Haml and can\'t seem to find a way to escape the & so that it stays just an & instead of

相关标签:
3条回答
  • 2020-12-20 04:48

    In my opinion, I don't like the idea to disable the feature to escape the characters generally. Maybe you use relay at some point in your application on it.

    For me the best way to do it is:

    %a{ href: '/', 'data-icon' => "✐".html_safe }
    
    0 讨论(0)
  • 2020-12-20 04:53

    I didn't like the top poster's way of completing this question. So far the best way I've found is to do:

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

    I'm not fully happy with this, but think it's better for rails apps rather than turning off HTML escaping everywhere.

    0 讨论(0)
  • 2020-12-20 04:55

    You can use the :escape_attrs option to control whether HTML sensitive characters in attributes are escaped:

    require 'haml'
    
    haml = "%a(href='/posts' data-icon=\"&#x0026\" aria-hidden='true')"
    
    puts Haml::Engine.new(haml, :escape_attrs => false).to_html
    

    Output:

    <a aria-hidden='true' data-icon='&#x0026' href='/posts'></a>
    

    Note that this will apply to all attributes in your Haml template.

    0 讨论(0)
提交回复
热议问题