Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js:使用多个小胡子模板_Node.js_Mustache - Fatal编程技术网

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

Node.js:使用多个小胡子模板,node.js,mustache,Node.js,Mustache,我正在尝试将一个小胡子模板分解成不同的组件,以便重用它们,并通过node.js返回组装好的文本。我找不到做过这件事的人 我可以返回必须包含以下内容的页面: function index(request, response, next) { var stream = mu.compileAndRender('index.mu', {name: "Me"} ); util.pump(stream, response); } 我就是不知道如何呈现一个

我正在尝试将一个小胡子模板分解成不同的组件,以便重用它们,并通过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);
}
{{> index.mu}}
但问题是页眉没有呈现在页面之前,即使我做到了这一点。标题被视为显示文本,而不是html


非常感谢您的帮助。

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


要包含原始HTML,您必须使用三个大括号:
{{{{raw}}
在您的模板中作为状态。

小胡子们给出了答案。我可以通过使用类似以下的部分来实现:

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);
}
{{> index.mu}}

在Moustach模板内部,而不是在node.js中尝试这样做。

我尝试了,但仍然遇到了问题。第一个问题是,如果我想包含多个其他模板,该怎么办。第二个问题是,包含的html以文本形式呈现