25

So what I want is to put some multiline HTML into a Pug file and can't find anywhere how to do this.

Example:

html
    head

    body
        <div><a href="lala"> blabla </a></div>

        p hihuhohoo

3 Answers 3

39

Pug text can include HTML. Just force it as text, and it should parse:

html
    head

    body
        | <div><a href="lala"> blabla </a></div>

        p hihuhohoo

Also, you were using backslashes, not forward slashes, to close elements.

2
  • 1
    Yeah the backslashes mistake was because I was making an example out of my head and haven't written actual html in forever :D
    – ditoslav
    Nov 24, 2014 at 15:34
  • 2
    THIS MADE MY DAY. Can also be used with parameters as in | !{paramName}
    – Schuere
    Aug 8, 2019 at 11:47
25

This is a tested example with passing variables with raw html to the pug file:

yourSourceFile.js

const p1 = 'This server uses a <a href="https://pugjs.org/api/getting-started.html" target="_blank">pug template</a> for the html output'
res.render('yourTemplateFile', { title: 'Hey', p1 })

yourTemplateFile.pug

html
  head
    title= title
  body
    p
      | !{p1}
1
  • 1
    That's exactly what I was looking for. Thank you! Nov 30, 2020 at 14:15
0

I was only able to do this by encoding < and >

div
  | &#60;script src="https://mydomain.io/script.js"&#x3E;&#60;/script&#x3E;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.