多行的换行似乎不适合我:
类似的东西:
intro: |
We are happy that you are interested in
and
more
和+更多需要在换行符上,但它失败了。
intro: |
| We are happy that you are interested in
| and
| more
或
intro: |
We are happy that you are interested in \n
and
more <2 spaces >
another one
一切都失败了。
如何在yaml文本块中正确使用多行?
我在rails app中的HAML视图中使用它,如
= t(“mailer.beta_welcome.intro”)
但是没有以这种方式打印换行符,我是否需要以raw或者其他方式输出它?
答案 0 :(得分:3)
您的第一个示例正常
foo.yml
intro: |
We are happy that you are interested in
and
more
foo.rb
require 'yaml'
puts YAML.load_file('foo.yml').inspect
输出
{"intro"=>"We are happy that you are interested in\nand \nmore\n"}
答案 1 :(得分:1)
呃..在挖掘了更多不同的关键词后,我发现了
= simple_format(t("mailer.beta_welcome.intro"))
虽然这看起来很愚蠢,但我现在看不到任何解决方法
答案 2 :(得分:1)
Google员工的迟到答案:
看起来您试图将其输出为HTML,这意味着如果您要检查页面,它确实输出了换行符。然而,HTML很大程度上忽略了空格,因此HTML渲染器将您的换行符和空格转换为空格。
根据simple_format
docs,simple_format将一些简单的格式规则应用于文本输出,以使其更接近明文输出 - 显然,将换行转换为&lt; br /&gt ;代码
所以你的问题与YAML没有任何关系,YAML的表现与预期一致。这实际上是因为HTML的工作方式,这也是预期的。 simple_format
修复了它,因为它从YAML获取了带有换行符的字符串并将其转换为带有&lt; br /&gt;的字符串标记,以便新行实际显示在HTML中,这是您首先想要的。
答案 3 :(得分:0)
你可以把你的字符串放在单引号中,它可以帮助我:
简介:'我们很高兴您对此感兴趣
和
更“