0

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.

esengineer
  • 9,514
  • 7
  • 45
  • 69

2 Answers2

3

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>
Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • Which version of Jade this should work in? I have 0.26.3, and the above syntax didn't work: compilation error. – esengineer Sep 27 '13 at 01:24
  • Right, that's what I did. Please add the minimum version requirement, and I will accept your answer. Thank you! – esengineer Sep 27 '13 at 06:10
  • its in the documentation at least 2 years - https://github.com/visionmedia/jade/commit/8eef32c5877e582d7943cd39075b7f364a6c82f3 . ( v0.22.1 should be enough ) Are you sure that you have version older than 2 years? – Andrey Sidorov Sep 27 '13 at 06:40
0

Even though I may accept the above answer which seems to have worked but haven't. Nevertheless, the following code did work.

!!! 5
<html>
head
    meta(http-equiv="Content-Type",content="text/html;charset=utf-8")
    meta(http-equiv="Cache-Control", content="no-cache")
    meta(http-equiv="Expires", content="0")
    title= title
    link(rel="shortcut icon",href='/img/nelo2_logo.ico')
    link(rel='stylesheet',href='/css/bootstrap.css')
    link(rel='stylesheet',href='/css/jquery-ui.css')
    link(rel='stylesheet',href='/css/chosen.css')
    link(rel='stylesheet',href='/css/common.css')
    link(rel='stylesheet',href='/css/style.css')
    script(src='/js/core/require.js')
<body>

So the solution was not to indent the <html> and <body> tags but have them on the same column. This way Jade doesn't close them.

esengineer
  • 9,514
  • 7
  • 45
  • 69