35
#test
  - html = "<a href='http://www.a.com'>Test</a>"
  = html

The output is

<div id='test'>
  &lt;a href='http://www.a.com'&gt;Test&lt;/a&gt;
</div>

But I want a output:

<div id='test'>
  <a href='http://www.a.com'>Test</a>
</div>

Anyone has solutions? Thanks.

garrydou
  • 353
  • 1
  • 3
  • 5

3 Answers3

35

haml also provides with its equivalent tag to rails 3's raw method: !=, eg:

!= html

James Chen
  • 10,794
  • 1
  • 41
  • 38
34
= raw html

or:

= html.html_safe
gunn
  • 8,999
  • 2
  • 24
  • 24
  • 2
    `html_safe` only works for strings, that's why I prefer `!=` (see @james-chen's answer below or `raw`. – iGEL Jun 06 '13 at 09:09
  • Is there a notable difference between `= raw html`, `= html.html_safe`, and `!= html`? **Edit:** [This question covers some differences](http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html). – Dennis Jun 17 '14 at 20:37
6

There was a change in rails 3 so that all content is now html escaped by default. See this blog post from Yehuda Katz for more details.

In order to print the html directly you need to use html_safe on your variable:

#content
 .title
   %h1= @title
   = @content.html_safe

For a more complex example, see this answer to a similar question.

Community
  • 1
  • 1
tomeduarte
  • 2,803
  • 1
  • 17
  • 10