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

陌路散爱 提交于 2019-11-29 12:08:16

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.

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.

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' => "&#10000;".html_safe }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!