Search code examples
node.jspugtemplate-engine

How to make so that Jade (Node.js) doesn't close the <body> tag


I'm trying to send multiple chunks of data to a client each of which is rendered by Jade templating engine in Express Node.js framework.

  1. I have several views like header, viewA, viewB, viewC, etc.
  2. For every request I need to render the header partial view and send it as a chunk so that the client browser starts rendering it. When the header view is rendered I don't want the <body> tag inside to be closed, because more data is to come which should be inside the <body> tag.
  3. In the meantime, I need to do some computations and after that render another view: either A, B, or C.
  4. Once A, B, or C view is sent, I close the response which means closing the </body></html> tags.

Sounds very simple. But the problem is that Jade closes the <html> and <body> tags when rendering the header view.

I know how to do this manually using native Node.js response object, however, can't figure out how to do this with Jade the right way.

The only solution I currently see is to manually send the header part down to open <body> tag, then render the rest as Jade partials via res.partial().

Any hint is highly appreciated.


Solution

  • You can output any text (including raw html) with !=

    != "<body>"
      some
         more
            tree(attr=1)
    

    Result in this output:

    <body>
    <some>
      <more>
        <tree attr="1"></tree>
      </more>
    </some>