How to insert raw HTML in Pug file (not include external HTML file)
Asked Answered
R

3

26

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
Rapport answered 24/11, 2014 at 14:44 Comment(0)
H
41

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.

Hyperventilation answered 24/11, 2014 at 14:47 Comment(2)
Yeah the backslashes mistake was because I was making an example out of my head and haven't written actual html in forever :DRapport
THIS MADE MY DAY. Can also be used with parameters as in | !{paramName}Pouched
P
29

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}
Paperboard answered 21/4, 2019 at 4:39 Comment(1)
That's exactly what I was looking for. Thank you!Sherbet
S
0

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

div
  | &#60;script src="https://mydomain.io/script.js"&#x3E;&#60;/script&#x3E;
Shelba answered 29/1, 2019 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.