如何在 Node.js 中为我的视图之一排除或指定备用layout.mustache?

问题描述 投票:0回答:1

当前所有页面均由

views/layout.mustache
文件和特定于页面的
views/page.mustache
模板

呈现

在我的应用程序中渲染某个视图时,我想使用备用

layout.mustache
和/或跳过
layout.mustache
。执行此操作的理想方法是什么?

这是我的

app.configure
的片段:

app.configure(function(){
    ...
    app.set('views', __dirname + '/views');
    app.register(".mustache", require('stache'));
    ...
});
node.js express templates mustache
1个回答
1
投票

您使用的是哪个版本的express?在 v3.0 中,布局的概念被删除了

V3.0引入了的概念。我不使用

mustache
但玉的示例如下所示:

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

或禁用某些视图的布局

res.render('start', {
  title : 'Page without layout', 
  layout : false
});
© www.soinside.com 2019 - 2024. All rights reserved.