Rendering a jade template with a layout (no express)

When rendering jade templates in express, you can customize your application using the 'view options', { layout: true } and the created templates are automatically connected to the local body template.

I am trying to get equivalent behavior rendering files from node.js, but without an explicit structure (I just create static files as part of a larger pipeline).

There seem to be two options:

  • Download both the main template and the layout, convert to functions, first create a template, and then pass the results to the layout function.
  • Use standard template inheritance and block structure, but then I explicitly use named blocks

Are these the only options (which, rightly, are still amazing), or did I miss some trick?


Edit

Here's a rough cut of the first option in case anyone is interested:

 // Load jade var jade = require('jade'); // Load actual template text var layout = fs.readFileSync('layout-path', 'utf8') tpl = fs.readFileSync('tpl-path', 'utf8'); // Compile template rendering function layout = jade.compile(layout, { pretty: true, filename: 'layout-path' }); tpl = jade.compile(tpl, { pretty: true, filename: 'tpl-path' }); // Render jade template, passing in the info var output = layout({ body: tpl({ local1: some_var, local2: some_var }) } // Write rendered content to file fs.writeFileSync('output.html', output); 
+6
source share
1 answer

I believe the answer is no, you won’t miss any trick. The two options you outline provide me with the two easiest ways to use jade to create your file. Of course, there are many non-jade approaches. For example, you can combine the contents using the scoreboard , the good old String.replace or split the layout into separate fragments of the header header fragments and simply connect them to the head, body, foot order.

+3
source

Source: https://habr.com/ru/post/909096/


All Articles