目前,所有页面均通过 views/layout.mustache
文件和特定于页面的 views/page.mustache
模板呈现
在我的应用程序中渲染某个 View 时,我想使用备用 layout.mustache
和/或完全跳过 layout.mustache
。执行此操作的理想方法是什么?
这是我的 app.configure
的片段:
app.configure(function(){
...
app.set('views', __dirname + '/views');
app.register(".mustache", require('stache'));
...
});
谢谢
最佳答案
您使用的是哪个版本的express?在 v3.0 中,布局的概念是 removed .
V3.0引入了blocks的概念。我不使用 mustache
,但使用 Jade 的示例如下所示:
// my-template.jade
extends my-layout
block head
script(src="myfile.js")
block content
h1 My page
和
// my-layout.jade
doctype 5
html
head
title My title
block head
body
#content
block content
使用extends
,您可以选择您想要的任何“父”布局。支持的模板引擎的概述在 express wiki 中。 .
在 3.0 之前的 Express 版本中,您可以在渲染时指定布局
res.render('index', {
title : 'Page with distinct layout',
layout : 'layout.mustache'
});
或禁用某些 View 的布局
res.render('start', {
title : 'Page without layout',
layout : false
});
关于node.js - 模板/渲染 - 如何为我的 View 之一排除或指定备用layout.mustache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044884/