首页 > 解决方案 > en.yml 中的新行不会在 html 页面上呈现

问题描述

在 en.yml 我有带有新行的字符串。当它们在 html 页面上呈现时,新行会消失——它被呈现为单行。

我的en.yml:

test: |+
  Something fdsafdsfd


  fdsafdsfd

标签: ruby-on-railsrubyruby-on-rails-5haml

解决方案


\nYAML为该test键返回一个字符串。如果你想在 html 中显示它,你需要将新行转换为正确的 html 标签。您可以为此使用simple_format助手:

simple_format("foo\nbar") # => "<p>foo\n<br />bar</p>"
simple_format("foo\n\nbar") # => "<p>foo</p>\n\n<p>bar</p>"

推荐阅读