Search code examples
ruby-on-railsescapinghamlhtml-escape-characters

Haml without Rails - unescape string


I'm using Haml outside of Rails and need to print out an unescaped string:

%div{ foo: '<?php echo "bar" %>' }

becomes

<div foo='&lt;?php echo "bar" %&gt;'></div>

but I want

<div foo='<?php echo "bar" %>'></div>

raw and html_safe are not defined outside of Rails, I tried requiring them and mixing them in, but the end result was the same. Maybe I'm missing something obvious here...


Solution

  • You need to set the escape_attrs option to false.

    From the command line you can use something like

    $ haml --no-escape-attrs my_file.haml
    

    or from Ruby something like:

    Haml::Engine.new(my_template, :escape_attrs => false).to_html