YAML 中包含引号的多行字符串
Multi-line string in YAML containing quotes
我有一个 YAML 配置文件,其中可以包含一些 CSS 用于自定义目的。
css.custom: >
.company-logo {
height: 60px;
padding-top: 15px;
}
.input[type=\"text\"] {
background: white;
border: 1px solid gray;
border-radius: 3px;
}
然后在名为 base.css.twig
的文件中呈现,如下所示:
{{ css.custom }}
我的问题是我无法让 input[type=\"text\"]
中的引号正确呈现,因为反斜杠按字面意思呈现,而引号呈现为 "
。有没有人想出如何成功地呈现原始报价?
生成的渲染:
input[type="text"]
您无法在 YAML multi-line 标量字符串中转义任何内容。您不必转义引号,因此您应该省略 \
:
css.custom: >
.company-logo {
height: 60px;
padding-top: 15px;
}
.input[type="text"] {
background: white;
border: 1px solid gray;
border-radius: 3px;
}
您还应该考虑您是否真的想要折叠 (>
) 而不是 multi-line 标量的文字 (|
) 样式。
folding style is like literal style:
The folded style is denoted by the “>” indicator. It is similar to the literal style; however, folded scalars are subject to line folding.
和the literal style没有转义:
There is no way to escape characters inside literal scalars. This restricts them to printable characters. In addition, there is no way to break a long literal line.
解决方案是使用{{ css.custom|raw }}
我以为我已经在使用 raw
但我错了。
我有一个 YAML 配置文件,其中可以包含一些 CSS 用于自定义目的。
css.custom: >
.company-logo {
height: 60px;
padding-top: 15px;
}
.input[type=\"text\"] {
background: white;
border: 1px solid gray;
border-radius: 3px;
}
然后在名为 base.css.twig
的文件中呈现,如下所示:
{{ css.custom }}
我的问题是我无法让 input[type=\"text\"]
中的引号正确呈现,因为反斜杠按字面意思呈现,而引号呈现为 "
。有没有人想出如何成功地呈现原始报价?
生成的渲染:
input[type="text"]
您无法在 YAML multi-line 标量字符串中转义任何内容。您不必转义引号,因此您应该省略 \
:
css.custom: >
.company-logo {
height: 60px;
padding-top: 15px;
}
.input[type="text"] {
background: white;
border: 1px solid gray;
border-radius: 3px;
}
您还应该考虑您是否真的想要折叠 (>
) 而不是 multi-line 标量的文字 (|
) 样式。
folding style is like literal style:
The folded style is denoted by the “>” indicator. It is similar to the literal style; however, folded scalars are subject to line folding.
和the literal style没有转义:
There is no way to escape characters inside literal scalars. This restricts them to printable characters. In addition, there is no way to break a long literal line.
解决方案是使用{{ css.custom|raw }}
我以为我已经在使用 raw
但我错了。