I'm trying to break a moustache template up into various components so I can reuse them, and get the assembled text returned through node.js. I cannot find anyone that has done this.
I can return must ache pages imply with:
function index(request, response, next) {
var stream = mu.compileAndRender('index.mu',
{name: "Me"}
);
util.pump(stream, response);
}
I just cannot figure out how to render a template and use it in another template. I've tried rendering it separately like this:
function index(request, response, next) {
var headerStream = mu.compileAndRender('header.mu', {title:'Home page'});
var headerText;
headerStream.on('data', function(data) {
headerText = headerText + data.toString();
});
var stream = mu.compileAndRender('index.mu',
{
heading: 'Home Page',
content: 'hello this is the home page',
header: headerText
});
util.pump(stream, response);
}
But the problem is that the header is not rendered before the page and even if I get that to happen. The header is seen as display text rather than html.
Any help appreciated.
You need to put the lines var stream = ...
and util.pump(...
in headerStream.on('end', function () { /* here */ });
, so that they are ran after the headerStream
has sent all the data to your data
listener.
To include raw HTML you have to use the triple braces: {{{raw}}}
in your template as http://mustache.github.com/mustache.5.html states.
The moustache guys came back with the answer. I can do it by using partials like this:
{{> index.mu}}
Inside the moustache template rather than trying to do it in node.js.