1

Here is my Haml:

%a{:href => "/settings", "data-icon" => "⚙"}

Which outputs:

<a data-icon='&amp;#9881;' href='/settings'>Settings</a>

I want to output:

<a data-icon='&#9881;' href='/settings'>Settings</a>

So I've tried escaping the ampersand:

%a{:href => "/settings", "data-icon" => "\&#9881;"}

But it seems escapes only work for the first character of a line.

I've also tried different methods of interpolation/escaping with no success:

"#{"&#9881;"}"

Using plain html (<a href="/settings" data-icon="&#9881">Settings</a>) is a stopgap measure that I do not consider a solution. What if I want to set the data-icon programatically?

Another option is:

%a{:href => "#", "data-icon" => "⚙"} Settings

But what if I want to use PUA characters? It also makes it a lot harder to tell what the character is in the markup.

Demo

4

1 回答 1

1

You need to set the escape_attrs option to false or :once.

$ haml --no-escape-attrs
%a{:href => "/settings", "data-icon" => "&#9881;"}

output:

<a data-icon='&#9881;' href='/settings'></a>

--no-escape-attrs sets the escape_attrs option to false from the command line. See the docs for info on how to set options in other cases.

(It doesn’t look like codepen.io lets you specify Haml options, so I can’t provide a demo there).

于 2012-11-28T12:32:51.867 回答