Node.js:使用多个胡子模板

时间:2012-05-22 02:57:31

标签: node.js mustache

我正在尝试将胡子模板分解为各种组件,以便我可以重用它们,并通过node.js返回汇编的文本。我找不到任何人这样做过。

我可以返回必须疼痛的页面暗示:

function index(request, response, next) {
    var stream = mu.compileAndRender('index.mu', 
        {name: "Me"}
        );
    util.pump(stream, response);
}

我无法弄清楚如何渲染模板并在另一个模板中使用它。我试过像这样单独渲染它:

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);
}

但问题是标题不会在页面之前呈现,即使我发生了这种情况。标题被视为显示文本而不是html。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

您需要将var stream = ...util.pump(...行放在headerStream.on('end', function () { /* here */ });中,以便在headerStream将所有数据发送到data后运行它们听众。

要包含原始HTML,您必须使用模板中的三个大括号{{{raw}}}作为http://mustache.github.com/mustache.5.html个州。

答案 1 :(得分:0)

小胡子男人回答了答案。我可以通过使用这样的部分来实现:

{{> index.mu}}

在小胡子模板中,而不是尝试在node.js中执行此操作。