Search code examples
javascriptnode.jspug

Printing variable without wrapping tag in Pug


I want to print some arbitrary content within tag. The content should be set from Javascript. Here's a minimal example:

pug.compileFile("templates/question.pug")({
   headExtraContent: "<script>console.log('hello')</script>",
});
doctype
   head
      title Some page title
      // headExtraContent should go here in an unescaped form
   body
      ...

I've tried #{headExtraContent}, ={headExtraContent, none of those worked.

Is there a way to achieve this in Pug?


Solution

  • You can combine the unescaped string interpolation syntax Declan mentions with the text block in a tag technique also described in the pug documentation.

    In your case, it would look like this:

    doctype
      head
        title Some page title
        .
          !{headExtraContent}
      body
        ...
    

    When in doubt, the documentation will have what you need 99% of the time.