Phoenix 和具有多个子项的嵌套 content_tags 删除嵌套内容

kov*_*ack 3 html tags elixir phoenix-framework

我的项目中有这样的结构:

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
Run Code Online (Sandbox Code Playgroud)

并期望它生成这样的 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>
Run Code Online (Sandbox Code Playgroud)

然而,它给了我一些意想不到的东西(为了可读性,我添加了新的行——原来一切都在一行中):

<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>
Run Code Online (Sandbox Code Playgroud)

我不太明白,如何将两个嵌套的content_tags 连接成一个:safe字符串,同时使该字符串"<span><b>Some bold text</b>and nothing more</span>"安全且不会被转义。

kov*_*ack 6

看来我几乎想通了。这段代码应该看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)