express和jade,忽略渲染错误
所以,快递+玉石==酷。毫无疑问。但是,来自 django,我缺少 django 模板使用的故障模式。在 django 中,如果我尝试渲染一个不存在的变量,它只会忽略它并继续前进。虽然有时这确实很烦人,但大多数时候并非如此。因此,当我尝试使用express在jade中渲染变量时,如果该变量不存在/未定义,express会抱怨500错误。
500 TypeError: Jade:14
12. ' // main content, post, login, etc '
13. ' - if (data.session.user) '
14. ' != partial(\'dash') '
Jade:3
1. '- if (data.session.user)'
2. ' p #{data.session.user.id}'
3. ' p #{data.cookie.connect.sid}' Cannot read property 'connect' of undefined
我只是在这里进行实验,所以请忽略我要打印的内容。 :) 总之,有没有一种简单的方法可以告诉jade/express忽略模板中的错误并继续渲染,例如django模板系统中的行为。
So, express+jade == cool. No question there. However, coming from django, i'm missing the failure mode that the django templates used. In django, if I try to render a variable that doesn't exist, it just ignores it and moves on. While there are times this is REALLY annoying, most of the time it is not. As such, when I try to render a variable in jade with express, express complains with a 500 error if that variable doesn't exist/is undefined.
500 TypeError: Jade:14
12. ' // main content, post, login, etc '
13. ' - if (data.session.user) '
14. ' != partial(\'dash') '
Jade:3
1. '- if (data.session.user)'
2. ' p #{data.session.user.id}'
3. ' p #{data.cookie.connect.sid}' Cannot read property 'connect' of undefined
I'm just experimenting here, so ignore what i'm trying to print. :) In summary, is there an easy way to tell jade/express to ignore errors in the template and continue rendering, such as is the behavior in the django template system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,因为当 javascript 评估失败时,渲染器会抛出错误。
https://github.com/visionmedia/jade/blob/master /lib/jade.js#L197
您可以分叉该项目并添加一个选项来避免调用
rethrow
或至少通过包装res.render 让渲染失败:
Its not possible as the renderer throws an error when the evaluation of the javascript fails.
https://github.com/visionmedia/jade/blob/master/lib/jade.js#L197
You can fork the project and add an option to avoid the call to
rethrow
or at least let your render fail better by wrappingres.render
:您可以在 Jade 中测试变量是否存在:
它不是自动的,但我发现这是一个救星。
You can test for variable existence in Jade:
It's not automatic, but I found this to be a lifesaver.