翡翠当地人范围界定
在 Jade 模板中使用局部变量时,我遇到范围界定问题。我的代码是...
function(req, res) {
BlogPostModel.find({}, function(e, data) {
if (e) throw e;
posts = data;
var path = __dirname + "/view/admin/blog.jade",
template = fs.readFileSync(path, "utf8"),
options = { filename: path },
fn = jade.compile(template, options),
html = fn(posts);
res.end(html);
});
上面的代码渲染得很好,但是我必须将“data”设置为全局变量。我宁愿将“数据”直接传递到我的函数调用中。但是当我这样做时,我从 Jade 收到“变量未定义”错误。谁能告诉我为什么“数据”超出范围?
谢谢,
FBZ
I have a problem with scoping when using locals in my Jade template. My code is...
function(req, res) {
BlogPostModel.find({}, function(e, data) {
if (e) throw e;
posts = data;
var path = __dirname + "/view/admin/blog.jade",
template = fs.readFileSync(path, "utf8"),
options = { filename: path },
fn = jade.compile(template, options),
html = fn(posts);
res.end(html);
});
The above code renders fine, but I'm having to make 'data' a global variable. I would rather pass 'data' directly into my function call. But when I do that, I get a 'variable is not defined' error from Jade. Can anyone tell my why 'data' is out of scope?
Thanks,
FBZ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这根本不是范围界定问题。我不得不称我的论点为“当地人”。像这样...
Jade 接受任何名称的全局变量,但局部变量必须称为“locals”,这似乎很奇怪。无论如何,排序了。
So it wasn't a scoping problem at all. I had to call my argument 'locals'. Like this...
Seems strange that Jade accepts a global variable of any name, but a local variable must be called 'locals'. Anyway, sorted.