Search code examples
pug

How do I let the pug engine not to convert some !{} syntax?


If I want to quote the literal !{a} in the code. What should I do?

- var a = '5' // I have to use the variable name "a"
script.
  var str = "!{a}"

It will converts to:

<script>
  var a = "5";  // I want to have the string equals "!{a}", not "5"
</script>

Solution

  • You can use interpolation to output the string '!{a}':

    script.
      var str = "#{'!{a}'}"
    

    Result:

    <script>
      var str = "!{a}"
    </script>
    

    A bit weird, but works.


    Another possibility is to store the string '!{a}' to a variable and use interpolation to output it:

    - var foo = '!{a}'
    script.
      var str = "#{foo}"
    

    Result:

    <script>
      var str = "!{a}"
    </script>
    

    Addendum:

    I thought that escaping the interpolation with a backslash would work, but it doesn't because the ! is converted to a #:

    script.
      var str = "\!{a}"
    

    Result:

    <script>
      var str = "#{a}"
    </script>
    

    Looks like a bug to me.