Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix and nested content_tags with multiple children remove nested content

I have such structure in my project:

content_tag(:div, class: "some-class", role: "alert") do
  content_tag(:button, type: :button, class: "close") do
    content_tag(:span, class: "some-other-class") do
      {:safe, ["×"]}
    end
  end
  content_tag(:button, type: :button, class: "close") do
    content_tag(:span, class: "some-other-class") do
      {:safe, ["×"]}
    end
  end
  "<span><b>Some bold text</b>and nothing more</span>"
end

And expect it to generate such HTML:

<div class="some-class" role="alert">
  <button class="close" type="button">
    &times;
  </button>
  <button class="close" type="button">
    &times;
  </button>
  <span><b>Some bold text</b>and nothing more</span>
</div>

However, it gives me something unexpected (I've added new lines for readability - in original everything is in one line):

<div class="some-class" role="alert">
  <button class="close" type="button">
    &lt;span&gt;&lt;b&gt;Some bold text&lt;/b&gt;and nothing more&lt;/span&gt;
  </button>
</div>

I do not really understand, how to join two nested content_tags into one :safe string, at the same time making this string "<span><b>Some bold text</b>and nothing more</span>" safe and not be escaped.

like image 334
kovpack Avatar asked Nov 17 '25 07:11

kovpack


1 Answers

Looks like I've almost figured it out. This code should look almost like this:

content_tag(:div, class: "some-class", role: "alert") do
  [content_tag(:button, type: :button, class: "close") do
    content_tag(:span, class: "some-other-class") do
      {:safe, ["&times;"]}
    end
  end,
  content_tag(:button, type: :button, class: "close") do
    content_tag(:span, class: "some-other-class") do
      {:safe, ["&times;"]}
    end
  end,
  {:safe, ["<span><b>Some bold text</b>and nothing more</span>"]}]
end
like image 175
kovpack Avatar answered Nov 20 '25 01:11

kovpack