开发者

rails 3 html_safe confusion

开发者 https://www.devze.com 2023-02-14 21:36 出处:网络
How do I achieve this? In a controller @arr = [\"<one>\", \"<two>\"] In a view (haml) = @arr.join开发者_开发百科(\"<br>\")

How do I achieve this?

In a controller

@arr = ["<one>", "<two>"]

In a view (haml)

= @arr.join开发者_开发百科("<br>")

As you guess, "<br>" shouldn't be escaped. So the result would be like the following.

&lt;one%gt;<br>&lt;two%gt;

How do I do that?

Thanks.

Sam


Rails has this built in.

<%= safe_join(@arr, "<br />".html_safe) %>


You could roll your own:

arr = ["<one>", "<two>"]
''.html_safe.tap {|x|
  arr.each_with_index { |el, ix|
    x << el
    x << raw("<br/>") if ix < arr.size-1
  }
}

Also look at the Array.join code in Rails

0

精彩评论

暂无评论...
验证码 换一张
取 消