我对是否应该在YAML文件中的HTML标记内转义双引号感到困惑。
playing_with_cards_html: Playing your best game with %{card_type} between now and <span class="best-card">%{worst_card}</span>.
它看起来是有效的YAML,但如果我们为双引号添加转义字符:
playing_with_cards_html: Playing your best game with %{card_type} between now and <span class=\"best-card\">%{worst_card}</span>.
它还能正确呈现吗?
它可能无法正确呈现,除非加载YAML的程序在加载的字符串上执行一些反斜杠转义。
密钥playing_with_cards_html
的值是plain scalar。在这些标量中没有定义反斜杠转义:每个字符都按原样拍摄。因此,URL的第二个版本实际上有两个额外的字符(都是\
),并且可能无法按需要呈现。
只有当标量本身为double quoted(即以双引号开头和结尾)时,才能在YAML中转义反斜杠。在这种情况下,你必须用反斜杠来逃避标量中间的任何双引号。