Compiling Jade from an Existing JS Node Context

I am interested in using Jade to create static HTML files with dynamically generated data. I already have a set of JS scripts that create my data while working in Node. I would like this to be done to compute my data, and then compile my HTML page using the JS objects that I created.

eg.

var myArry = MyLib.calculateSomeArray(); var myObj = MyLib.createSomeObj(); jade.compile(myTemplate); 

Jade Template:

 each item in myArry li= item each val, key in myObj li #{key}: #{val} 

I assume that this is one of those cases where it is so obvious that I miss it in the documentation, but I do not see it, and the closest answers that I saw are related to Express, which seems to be unnecessary.

+4
source share
1 answer

I would say, given the documents :

 var jade = require('jade'); // Create the function var fn = jade.compile(myTemplate); var html = fn({ myArry: myArry, myObj: myObj }); 

Just tested and it works just fine:

 > var jade = require('jade'); undefined > var myTemplate = "each item in myArry\n\tli= item\neach val, key in myObj\n\tli #{key}: #{val}" undefined > var myObj = { foo: 'bar', woo:'loo' }; undefined > var myArry = ['moo', 'boo', 'roo']; undefined > var fn = jade.compile(tpl); undefined > fn({ myArry: myArry, myObj: myObj }); '<li>moo</li><li>boo</li><li>roo</li><li>foo: bar</li><li>woo: loo</li>' 

Is this what you want?

+6
source

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


All Articles