javascript - Node JS - 在 Jade 中扩展布局时出错

标签 javascript pug

如果我理解的话,当我使用 Jade 时,我可以将模板的某些部分移动到父模板吗?

我尝试将一些 js 文件的链接移至 head 部分,我仅将其用于网站的一部分,但出现错误。

我的layout.jade:

!!!
html(lang='en')
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/box.css')
    block scripts
  body
    ..

我的index.jade(仅供尝试,它将在其他文件中)

extends layout

block scripts
  script(src='myscriptfile.js')
div#sortable
  ..

以及节点服务器的某些部分:

app.get('/', function(req, res){
  res.render('index', 
    {title: 'My title',
    images: images}
  );
});

我切换此选项真/假

app.set('view options', { pretty: true });

但仍然出现错误:

Express
500 ReferenceError: E:\projekt/views/index.jade:13 11| 12| > 13| 14| body is not defined

我做错了什么?

最佳答案

在代码片段的 .. 中,您引用了尚未定义的 body 变量。我猜测在 index.jade 第 13 行:

500 ReferenceError: E:\projekt/views/index.jade:13 11| 12| > 13| 14| ...

您必须将其从 View 中删除或在 View 数据中定义它:

res.render('index', 
    {title: 'My title',
    images: images,
    body: '...'}
);
<小时/>

旁注:

如果您尚未升级到 Express 3.x,您可能需要确保禁用布局:

app.set('view options', {
  layout: false,
  pretty: true
});

继承和 block 是 meant to replace layouts ,不与他们合作。如 Migrating from 2.x to 3.x 中所述:

Removed... the concept of a "layout" (template engine specific now)

关于javascript - Node JS - 在 Jade 中扩展布局时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10823973/

相关文章:

javascript - 如何使用 JavaScript 将 html 代码插入我的页面?

javascript - 从嵌套在 div 中的 img 发送 src

html - : express + jade/ejs + html5 + css + websockets 的逻辑是什么

javascript - node.js - 表达 - res.render() : Correct format for feeding variables into the JSON parameter?

javascript - Node.js 异步查找具有相同内容的文件

javascript - passport-jwt 总是返回 401 unauthorized

javascript - 谷歌地图折线不显示和抛出错误

pug - 如何在 Jade 中取消转义 '&'

javascript - 在jade模板中显示mongodb集合

pug - 使用 Jade 作为 Durandal View 引擎?