如果我理解的话,当我使用 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/