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

时间:2016-07-08 21:38:22

标签: 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

并期望它生成这样的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>

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

<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>

我真的不明白,如何将两个嵌套的content_tag加入一个:safe字符串,同时使这个字符串"<span><b>Some bold text</b>and nothing more</span>"安全无法转义。

1 个答案:

答案 0 :(得分:3)

看起来我差不多想通了。这段代码看起来应该是这样的:

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