简体   繁体   中英

Multi-line string in YAML containing quotes

I have a YAML config file that can contain a bit of CSS for customization purposes.

css.custom: >
  .company-logo {
    height: 60px;
    padding-top: 15px;
  }
  .input[type=\"text\"] {
    background: white;
    border: 1px solid gray;
    border-radius: 3px;
  }

Which is then rendered in a file called base.css.twig like this:

{{ css.custom }}

My problem is that I can't get the quotes in input[type=\\"text\\"] to render correctly as the backslash is rendered literally and the quotes are rendered as " . Has anyone figured how to render raw quotes successfully?

The resulting render:

input[type="text"]

You cannot escape anything in YAML multi-line scalar strings. You don't have to escape the quotes, so you should leave out the \\ :

css.custom: >
  .company-logo {
    height: 60px;
    padding-top: 15px;
  }
  .input[type="text"] {
    background: white;
    border: 1px solid gray;
    border-radius: 3px;
  }

You should also consider if you really want the folding ( > ) and not the literal ( | ) style of multi-line scalars.

The 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.

and the literal style doesn't have escaping:

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.

The solution is to use {{ css.custom|raw }}

I thought I was already using raw but I was wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM