在YAML中,我有一个很长的字符串。我希望将其保存在编辑器的80列(或左右)视图中,因此我希望断开字符串。这是什么语法?
换句话说,我有这个:
Key: 'this is my very very very very very very long string'
我想要这个(或类似的东西):
Key: 'this is my very very very ' +
'long string'
我想使用上面的引号,所以我不需要转义字符串中的任何内容。
在YAML中,我有一个很长的字符串。我希望将其保存在编辑器的80列(或左右)视图中,因此我希望断开字符串。这是什么语法?
换句话说,我有这个:
Key: 'this is my very very very very very very long string'
我想要这个(或类似的东西):
Key: 'this is my very very very ' +
'long string'
我想使用上面的引号,所以我不需要转义字符串中的任何内容。
使用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.
在这两种情况下,每一个换行符都被一个空格替换。
还有其他可用的控制工具(例如,用于控制缩进)。
看见https://yaml-multiline.info/
要保留换行符,请使用|,例如:
|
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英寸
如果您在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: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
对于字符串可能包含空格或不包含空格的情况,我更喜欢双引号和带反斜杠的换行符:
key: "String \
with long c\
ontent"
但请注意,如果连续行以空格开头,则需要进行转义(因为它将在其他地方被剥离):
key: "String\
\ with lon\
g content"
如果字符串包含换行符,则需要用C样式\n编写。
另请参见此问题。
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"
}
有关更多信息,请访问我的博客
在Jekyll项目中的YAML文件中,上述解决方案都不适用于我。在尝试了许多选项后,我意识到使用<br>的HTML注入也可以,因为最终所有内容都呈现为HTML:
姓名:|在一个叫La Mancha的村庄<br>我不想记住他的名字<br>。
至少它对我有用。不知道与这种方法相关的问题。