yaml - 在YAML,字符串如何跨多行?

在YAML里我有一个很长的字符串,

换句话说,我有:


Key: 'this is my very very very very very very long string'

我想这样(或者这个效果):


Key: 'this is my very very very ' +
 'long string'

时间:

折叠样式是你所需要的:


Key: >
 this is my very very very 
 long string

this is my very very very long string

如果使用|"文字样式"方法,则换行字符将插入到每行的字符串中:


Key: |
 this is my very very very 
 long string

this is my very very verynlong string

下面是官方的定义YAML Spec 1.2

内容可以使用所有换行符都有效的文字样式(用表示)以块符号表示,或者,它们可以用折叠式(表示方式">")编写,其中每一行折叠都会折叠到一个空格。

这也是有效的YAML,称为"普通样式":


Key: this is my very very very 
 long string

就像这个"双引号式":


Key:"this is my very very very 
 long string"

甚至这个"单引号样式":


Key: 'this is my very very very
 long string'

但是,每种样式的精确语法规则略有不同。

要保留换行符,请使用|,例如:


|
 This is a very long sentence
 that spans several lines in the YAML
 but which will be rendered as a string
 with newlines preserved.

这是一个非常长的句子n,它跨越了几行,但是它将被呈现为一个字符串

因此,即使是以下代码:

var javascriptVariable ="{{- 'key'|trans -}}";

以下是以下yml翻译:


key: >
 This is a
 multi line 
 translation.

仍将在html中生成以下代码:


var javascriptVariable ="This is a multi line translation.
";

所以,Twig中的减号不能解决这个问题,解决方案是在yml sign后,添加这个减号:


key: >-
 This is a
 multi line 
 translation.

Twig中的一行将有正确的结果,多行翻译:


var javascriptVariable ="This is a multi line translation.";

...