1

I am trying to render the following line of code in haml

%p.article-body
  lorem
  = "<div>ipsum</div>".html_safe

I intend it to render as

<p class="article-body">
  lorem
  <div>ipsum</div>
</p>

But instead it renders as

<p class="article-body">
  lorem
</p>
<div>ipsum</div>

What am I doing wrong? It only happens to those lines with call to html_safe. Otherwise, the contents will render inside the parent element.

Martin Verdejo
  • 1,229
  • 2
  • 13
  • 24

4 Answers4

0

As HAML is indentation sensitive markup, you need to have proper indentation. Try this:

%p.article-body
  = "<div>lorem</div>".html_safe
RAJ
  • 9,697
  • 1
  • 33
  • 63
  • I tried this but the line with `html_safe` call still renders outside the parent element. It works for those without the `html_safe`. – Martin Verdejo Apr 15 '15 at 13:37
0

check your indentation. Please change the code like this

%p.article-body
  %div lorem
Jyothu
  • 3,104
  • 17
  • 26
  • I am actually calling html_safe on a field so the contents of `%p.article-body` will have to be dynamic. I do not plan to hard code the div tag. I tried the indentation but again, those with `html_safe` get rendered outside the element. – Martin Verdejo Apr 15 '15 at 13:47
0

Use this code:

%p.article-body

%div lorem.html_safe

Uday kumar das
  • 1,615
  • 16
  • 33
0

%p.article-body= "lorem".html_safe

This is wrong syntax. As <p> tag not allowed to contain <div> tag.

Why <p> tag can't contain <div> tag inside it?

Try <div> tag(by removing %p) instead <p> like this:

.article-body
  = "<div>lorem</div>".html_safe

OR

.article-body
  != "<div>lorem</div>"  # if you not wanted to use .html_safe

plz check this for more details about != :

For more Info / Learn HAML structure refer below links:

I hope this may helps you. :)

Community
  • 1
  • 1
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55