Monday, May 21, 2012

Node.js: Using multiple moustache templates

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.





No comments:

Post a Comment