将内插的 html 标记解释为 html,而不是字符串文字?
Interpret interpolated html tag as html, not string literal?
我正在尝试将 HTML 标记 <br>
解释为换行符,但当我尝试这样做时它在视图中显示为字符串文字:
<%= property.address_line_2 + "<br>" if property.address_line_2.present? %>
我试过raw()
and .html_safe
但效果一样。
<%= property.address_line_2 + raw("<br>") if property.address_line_2.present? %>
<%= property.address_line_2 + "<br>".html_safe if property.address_line_2.present? %>
是否有 elegant/idiomatic 方法可以做到这一点,或者使用另一条线是最好的方法吗?我认为这种方法不是那么干:
<%= property.address_line_2 if property.address_line_2.present? %>
<% if property.address_line_2.present? %><br><% end %>
我会用
<% if property.address_line_2.present? %>
<%= property.address_line_2 %><br>
<% end %>
这还有一个额外的好处,那就是更容易阅读
html_safe
必须返回字符串才能使标签不被转义:
<%= "<br>".html_safe %>
当 html_safe 字符串(又名 ActiveSupport::SafeBuffer
)连接到常规字符串(又名 String
)时,结果是常规未转义的字符串:
>> "<br>".html_safe.class
=> ActiveSupport::SafeBuffer
>> ("address" + "<br>".html_safe).class
=> String
>> ("address" + "<br>".html_safe).html_safe?
=> false
raw
只是一个辅助方法,做同样的事情,但首先调用 to_s
:.to_s.html_safe
>> include ActionView::Helpers::OutputSafetyHelper
>> ("address" + raw("<br>")).html_safe?
=> false
>> raw("address" + "<br>").html_safe?
=> true
# NOTE: this doesn't work
>> ("address".html_safe + "<br>").html_safe?
=> true
# but the tag is escaped
>> "address".html_safe + "<br>"
=> "address<br>"
如果您必须将标签连接为字符串,请将整个结果标记为 html 安全:
<%= (property.address_line_2 + "<br>").html_safe %>
<%= property.address_line_2.html_safe + "<br>".html_safe %>
<%= raw(property.address_line_2 + "<br>") %>
<%== property.address_line_2 + "<br>" %>
如果您不想冒险使整个字符串安全,但仍想添加标签,请使用 safe_join
帮助程序:
<%= safe_join [property.address_line_1, "<br>".html_safe] %>
一次完成整个地址:
<% address_1 = "1st Street NE"; address_2 = " "; address_3 = nil; city = "City"; state = "Sate"; country = "Country" %>
<%=
safe_join(
[
address_1,
address_2,
address_3,
[city, state].join(", "),
country
].reject(&:blank?),
tag.br
)
%>
# returns html safe string
"1st Street NE<br>City, Sate<br>Country"
# and renders
1st Street NE
City, Sate
Country
也只使用简单的 erb:
<% if (line = property.address_line_1.presence) %>
<%= line %>
<br>
<% end %>
https://api.rubyonrails.org/classes/String.html#method-i-html_safe
https://api.rubyonrails.org/classes/ActionView/Helpers/OutputSafetyHelper.html#method-i-raw
https://api.rubyonrails.org/classes/ActionView/Helpers/OutputSafetyHelper.html#method-i-safe_join
https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag
我正在尝试将 HTML 标记 <br>
解释为换行符,但当我尝试这样做时它在视图中显示为字符串文字:
<%= property.address_line_2 + "<br>" if property.address_line_2.present? %>
我试过raw()
and .html_safe
但效果一样。
<%= property.address_line_2 + raw("<br>") if property.address_line_2.present? %>
<%= property.address_line_2 + "<br>".html_safe if property.address_line_2.present? %>
是否有 elegant/idiomatic 方法可以做到这一点,或者使用另一条线是最好的方法吗?我认为这种方法不是那么干:
<%= property.address_line_2 if property.address_line_2.present? %>
<% if property.address_line_2.present? %><br><% end %>
我会用
<% if property.address_line_2.present? %>
<%= property.address_line_2 %><br>
<% end %>
这还有一个额外的好处,那就是更容易阅读
html_safe
必须返回字符串才能使标签不被转义:
<%= "<br>".html_safe %>
当 html_safe 字符串(又名 ActiveSupport::SafeBuffer
)连接到常规字符串(又名 String
)时,结果是常规未转义的字符串:
>> "<br>".html_safe.class
=> ActiveSupport::SafeBuffer
>> ("address" + "<br>".html_safe).class
=> String
>> ("address" + "<br>".html_safe).html_safe?
=> false
raw
只是一个辅助方法,做同样的事情,但首先调用 to_s
:.to_s.html_safe
>> include ActionView::Helpers::OutputSafetyHelper
>> ("address" + raw("<br>")).html_safe?
=> false
>> raw("address" + "<br>").html_safe?
=> true
# NOTE: this doesn't work
>> ("address".html_safe + "<br>").html_safe?
=> true
# but the tag is escaped
>> "address".html_safe + "<br>"
=> "address<br>"
如果您必须将标签连接为字符串,请将整个结果标记为 html 安全:
<%= (property.address_line_2 + "<br>").html_safe %>
<%= property.address_line_2.html_safe + "<br>".html_safe %>
<%= raw(property.address_line_2 + "<br>") %>
<%== property.address_line_2 + "<br>" %>
如果您不想冒险使整个字符串安全,但仍想添加标签,请使用 safe_join
帮助程序:
<%= safe_join [property.address_line_1, "<br>".html_safe] %>
一次完成整个地址:
<% address_1 = "1st Street NE"; address_2 = " "; address_3 = nil; city = "City"; state = "Sate"; country = "Country" %>
<%=
safe_join(
[
address_1,
address_2,
address_3,
[city, state].join(", "),
country
].reject(&:blank?),
tag.br
)
%>
# returns html safe string
"1st Street NE<br>City, Sate<br>Country"
# and renders
1st Street NE
City, Sate
Country
也只使用简单的 erb:
<% if (line = property.address_line_1.presence) %>
<%= line %>
<br>
<% end %>
https://api.rubyonrails.org/classes/String.html#method-i-html_safe
https://api.rubyonrails.org/classes/ActionView/Helpers/OutputSafetyHelper.html#method-i-raw
https://api.rubyonrails.org/classes/ActionView/Helpers/OutputSafetyHelper.html#method-i-safe_join
https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag