【问题标题】:Phoenix and nested content_tags with multiple children remove nested contentPhoenix 和带有多个子项的嵌套 content_tags 删除嵌套内容
【发布时间】:2017-03-06 11:57:42
【问题描述】:

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

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_tags 连接成一个:safe 字符串,同时使这个字符串"&lt;span&gt;&lt;b&gt;Some bold text&lt;/b&gt;and nothing more&lt;/span&gt;" 安全且不会被转义。

【问题讨论】:

    标签: html tags elixir phoenix-framework


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 2022-10-24
      • 2015-08-15
      • 1970-01-01
      • 2015-05-28
      • 2021-11-27
      相关资源
      最近更新 更多