36

I have a set of html files, mostly static, I would like to move to my node.js/express/jade project. What's the right way to include html file or snippet directly in jade? I don't want to translate existing html file to jade?

jason chen
  • 872
  • 2
  • 8
  • 10

3 Answers3

49

You should be able to simply include it within a jade template:

As mentioned include can be used to include other content such as html or css. By providing an extension, Jade will read that file in, apply any filter matching the file's extension, and insert that content into the output.

html
  // ...
  body
    // ...
    //- html files have no filter and are included verbatim
    include content.html
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
7

Use :verbatim before the exact html code or snippet directly in jade.

doctype html
html(lang="en")
  :verbatim
    {% include head.html %}
  body
    :verbatim
    {{ content }}

  :verbatim
    {% include footer.html %}

Output

<!DOCTYPE html>
<html lang="en">{% include head.html %}
  <body>{{ content }}
  </body>{% include footer.html %}
</html>
XY L
  • 25,431
  • 14
  • 84
  • 143
  • 1
    You are awesome to know about :verbatim. I can not find this in the jade-lang.com documentation, but it certainly works. It's the only reasonably maintainable way I've found to include multi-line code fragments and other pre-formatted text in Jade. – matty May 24 '15 at 06:15
  • 1
    What version does :verbatim apply to pls? Jade 1.11.0 doesnt understand that declaration – Owen Beresford Nov 18 '15 at 16:26
  • 2
    The new version of Jade, which was renamed to Pug, does not understand :verbatim. – nomad Dec 29 '16 at 17:01
4

In my .jade file, I had to do something like this:

:verbatim
   !{editorBody}

.. where editorBody is provided via res.render() call :

var editorBody = '<p>Hello</p>';

return res.render('user/user_profile', {editorBody : editorBody});
Gene Bo
  • 11,284
  • 8
  • 90
  • 137