node.js - 模板/渲染 - 如何为我的 View 之一排除或指定备用layout.mustache?

标签 node.js templates express connect mustache

目前,所有页面均通过 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/

相关文章:

node.js - 为什么表示加密后解密文件的流的行为与表示原始文件的流不同?

node.js - 这是创建全局 const 变量的正确方法吗?

templates - 避免 groovy 模板中的新断线

不提供模板参数的 C++ 类指针

javascript - 快速路由正则表达式

node.js - 处理node.js分析时 "Code move event for unknown code"是什么意思?

javascript - 如果数组中的任何项 === x 则返回 false

c++ - 使用模板化是否为带有自定义键的 unordered_map/set 制作了一个好的模式

javascript - Angular NgRoute "/"有效,但其余部分无效?

javascript - axios.get 请求返回 GET,HEAD 作为响应