Sam Fen
Sam Fen

Reputation: 5254

Compiling Jade from within an existing JS Node context

I am interested in using Jade to create static HTML files of dynamically-generated data. I already have a set of JS scripts that are creating my data while running in Node. What I would like would be able to do is to calculate my data and then compile my HTML page using those JS objects I created.

e.g.

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 am assuming that this is one of those cases where it's so obvious that I'm missing it in the documentation, but I'm not seeing it and the closest answers I've seen appear to involve Express, which seems like it ought to be unnecessary.

Upvotes: 4

Views: 1416

Answers (1)

Pierre
Pierre

Reputation: 6172

I'd say, given the docs:

var jade = require('jade');

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

Just tested, and this is working 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 that what you want?

Upvotes: 6

Related Questions