Answers:
使用yaml折叠样式,每个换行符都被一个空格代替。每行的缩进将被忽略。一个换行符将插入到末尾。
Key: >
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with only a single carriage return appended to the end.
http://symfony.com/doc/current/components/yaml/yaml_format.html
您可以使用“块记录指示器”消除尾随换行符,如下所示:
Key: >-
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with NO carriage returns.
也有其他控制工具可用(例如,用于控制压痕)。
{{- 'key'|trans -}}
也不起作用。
在YAML中有5 6 NINE(或63 *,取决于您的计数方式)不同的方式来编写多行字符串。
通常,您想要>
:
key: >
Your long
string here.
如果您希望将换行符保留\n
在字符串中(例如,带段落的嵌入式markdown),请使用|
。
key: |
### Heading
* Bullet
* Points
如果您不希望在末尾附加换行符,请使用>-
或|-
代替。
如果您需要在单词中间分割行或直接将换行符键入为\n
,请使用双引号代替:
key: "Antidisestab\
lishmentarianism.\n\nGet on it."
YAML太疯狂了。
>
,|
)这些允许使用诸如\
和"
不进行转义的字符,并\n
在字符串的末尾添加新行()。
>
折叠样式会删除字符串中的单个换行符(但在末尾添加一个,并将双换行符转换为单行):
Key: >
this is my very very very
long string
→ this is my very very very long string\n
|
文字风格将字符串中的每个换行符转换为文字换行符,并在末尾添加一个:
Key: |
this is my very very very
long string
→ this is my very very very\nlong string\n
这是YAML Spec 1.2中的官方定义
标量内容可以使用文字样式(用“ |”表示)以块符号表示,其中所有换行符均有效。另外,也可以用折叠样式(用“>”表示)来书写,其中每个换行符都被折叠到一个空格,除非它以空行或缩进的行结束。
>-
,|-
,>+
,|+
)您可以\n\n
通过添加块标记指示符来控制字符串中最后一行的处理以及任何尾随的空白行():
>
,|
:“ clip”:保持换行,删除尾随的空白行。>-
,|-
:“ strip”:删除换行,删除尾随的空白行。>+
,|+
:“ keep”:保持换行,尾随空白行。
,"
,'
)这些具有有限的转义,并构造没有换行符的单行字符串。它们可以与键在同一行开始,也可以先添加其他换行。
普通样式(无转义,否#
或:
组合,首字符限制):
Key: this is my very very very
long string
用双引号引起来的样式(\
并且"
必须用进行转义\
,换行符可以按原义\n
顺序插入,行可以串联而后没有空格\
):
Key: "this is my very very \"very\" loooo\
ng string.\n\nLove, YAML."
→ "this is my very very \"very\" loooong string.\n\nLove, YAML."
单引号样式(字面量'
必须加倍,没有特殊字符,可能对表示以双引号开头的字符串很有用):
Key: 'this is my very very "very"
long string, isn''t it.'
→ "this is my very very \"very\" long string, isn't it."
在此表中,_
是space character
。\n
表示“换行符”(\n
在JavaScript中),但“行内换行”行除外,该行在字面上表示反斜杠和n)。
> | " ' >- >+ |- |+
-------------------------|------|-----|-----|-----|------|------|------|------
Trailing spaces | Kept | Kept | | | | Kept | Kept | Kept | Kept
Single newline => | _ | \n | _ | _ | _ | _ | _ | \n | \n
Double newline => | \n | \n\n | \n | \n | \n | \n | \n | \n\n | \n\n
Final newline => | \n | \n | | | | | \n | | \n
Final dbl nl's => | | | | | | | Kept | | Kept
In-line newlines | No | No | No | \n | No | No | No | No | No
Spaceless newlines| No | No | No | \ | No | No | No | No | No
Single quote | ' | ' | ' | ' | '' | ' | ' | ' | '
Double quote | " | " | " | \" | " | " | " | " | "
Backslash | \ | \ | \ | \\ | \ | \ | \ | \ | \
" #", ": " | Ok | Ok | No | Ok | Ok | Ok | Ok | Ok | Ok
Can start on same | No | No | Yes | Yes | Yes | No | No | No | No
line as key |
请注意“空格”之前一行的尾随空格。
- >
very "long"
'string' with
paragraph gap, \n and
spaces.
- |
very "long"
'string' with
paragraph gap, \n and
spaces.
- very "long"
'string' with
paragraph gap, \n and
spaces.
- "very \"long\"
'string' with
paragraph gap, \n and
s\
p\
a\
c\
e\
s."
- 'very "long"
''string'' with
paragraph gap, \n and
spaces.'
- >-
very "long"
'string' with
paragraph gap, \n and
spaces.
[
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.\n",
"very \"long\"\n'string' with\n\nparagraph gap, \\n and \nspaces.\n",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces."
]
以防万一上述内容不足以满足您的需要,您可以添加一个“ 块缩进指示器 ”(如果您有一个块,则在块切割指示器之后):
- >8
My long string
starts over here
- |+1
This one
starts here
如果您以折线样式在非第一行的开头插入多余的空格,则会保留这些空格,并带有额外的换行符。流样式不会发生这种情况:
- >
my long
string
- my long
string
→ ["my long\n string\n", "my long string"]
我什至不能
*
2种块样式,每种都有2种可能的块标记(或没有),以及9种可能的缩进指示(或没有),1种普通样式和2种带引号的样式:2 x(2 + 1)x(9 +1)+1 + 2 = 63
这些信息中的一些也已在此处进行了总结。
"..." + "..."
在大多数编程语言中会写什么,或者在Bash中在换行符前加反斜杠。
:
字符串数组中一个字符串内的纯冒号使YAML将其解释为对象数组。它违反了最小惊讶原则。
要保留换行符,请使用|
,例如:
|
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,它跨越YAML \ n中的几行,但是将被呈现为字符串 \ n并保留换行符。\ n “
|
在每一行上重复该操作,原因对我而言并不明显:groups.google.com/forum
cat
与定界符一起使用时,这会导致将前导空格(YAML必需)添加到输出中。
1.块符号(普通,流式,标量): 删除块后,换行符变为空格,多余的换行符
---
# Note: It has 1 new line after the string
content:
Arbitrary free text
over multiple lines stopping
after indentation changes...
...
等效JSON
{
"content": "Arbitrary free text over multiple lines stopping after indentation changes..."
}
2.文字块标量: 文字块标量|将包括换行符和任何尾随空格。但删除了多余的
换行符。
---
# After string we have 2 spaces and 2 new lines
content1: |
Arbitrary free text
over "multiple lines" stopping
after indentation changes...
...
等效JSON
{
"content1": "Arbitrary free text\nover \"multiple lines\" stopping\nafter indentation changes... \n"
}
3. +具有文字块标量的指示器:在块后保留额外的换行符
---
# After string we have 2 new lines
plain: |+
This unquoted scalar
spans many lines.
...
等效JSON
{
"plain": "This unquoted scalar\nspans many lines.\n\n\n"
}
4. –具有文字块标量的指示符: –表示删除字符串末尾的换行符。
---
# After string we have 2 new lines
plain: |-
This unquoted scalar
spans many lines.
...
等效JSON
{
"plain": "This unquoted scalar\nspans many lines."
}
5.折叠块标量(>):
会将换行符折叠到空格,但在块后删除多余的换行符。
---
folded_newlines: >
this is really a
single line of text
despite appearances
...
等效JSON
{
"fold_newlines": "this is really a single line of text despite appearances\n"
}
有关更多信息,请访问我的博客
您可能不相信,但是YAML也可以执行多行键:
?
>
multi
line
key
:
value
key:value
,但是如果您的密钥包含换行符,则可以如上所述进行操作
?
是关键指标(如映射中的关键一样)。在许多情况下,当键后的(必需)值指示符:
变得明确时,您可能会省略键指示符。但是事实并非如此,您必须使用它来显式标记密钥。
要连接没有空格的长行,请使用双引号,并使用反斜杠转义换行符:
key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
(感谢@Tobia)
如果您在Symfony中使用YAML和Twig进行翻译,并且想在Javascript中使用多行翻译,则在翻译后立即添加回车符。因此,即使是以下代码:
var javascriptVariable = "{{- 'key'|trans -}}";
其中yml翻译如下:
key: >
This is a
multi line
translation.
仍将导致以下html代码:
var javascriptVariable = "This is a multi line translation.
";
因此,Twig中的减号不能解决此问题。解决方案是在yml中的大于号后添加此减号:
key: >-
This is a
multi line
translation.
将获得正确的结果,在Twig中的一行上进行多行翻译:
var javascriptVariable = "This is a multi line translation.";
对于字符串可能不包含空格的情况,我更喜欢双引号和带有反斜杠的行继续:
key: "String \
with long c\
ontent"
但是请注意,对于连续线以空格开头的情况,陷阱是必须转义的(因为它将在其他地方被剥离):
key: "String\
\ with lon\
g content"
如果字符串包含换行符,则需要以C样式编写\n
。
另请参阅此问题。
在Jekyll项目中的YAML文件中,以上解决方案均不适用于我。在尝试了许多选项之后,我意识到使用HTML注入<br>
也可以这样做,因为最终所有内容都呈现为HTML:
姓名:|
在拉曼恰(La Mancha)村,<br>
我<br>
不想记住这个名字。
至少对我有用。不知道与这种方法相关的问题。