(不)将局部变量传递给Express中的Jade模板(node.js)((not) passing a local variables to Jade templates in Express (node.js))

这是我的场景:我有一个login.jade模板,我在其中验证用户身份。 在那个模板里面,我有几个if:

- if (badLogin) div#loginErr | <strong>Please try again</strong> | | The password or username you entered is incorrect. - if (loginError) div#loginErr | <strong>Please try again later</strong> | | Our authentication service isn't available at the moment.

在Express I中,我必须声明可能在该Jade模板中使用的所有局部变量。 否则我得到:

loginError is not defined

我的观点是,如果在Jade模板中我将包含一个包含局部变量的10'if'语句而不是渲染,我将不得不总是传递这10个变量,即使我只在该特定情况下使用一个。

我只是想如果我没有将任何变量传递给Jade模板,那么“if(var)”的结果将只是“false”。

有可能在这周围工作吗?

Here is my scenario: I have a login.jade template where I authenticate users. Inside that template I have a few if's like:

- if (badLogin) div#loginErr | <strong>Please try again</strong> | | The password or username you entered is incorrect. - if (loginError) div#loginErr | <strong>Please try again later</strong> | | Our authentication service isn't available at the moment.

In Express I ALWAYS have to declare ALL local variables that MIGHT be used in that Jade template. Otherwise I get:

loginError is not defined

My point is that if in Jade template I would have 10 'if' statements containing a local variable than while rendering I would have to always pass this 10 variables EVEN IF I would only use one in that particular case.

I just thought that if I do not pass any variable to Jade template that the result of "if(var)" would be just "false".

Any chances of working this around?

最满意答案

if (typeof loginError !== "undefined")

这允许您检查不存在的变量。

正如@GeoffChappel所提到的,我早些时候已经解决了这个问题

我确实改变了他们如何处理注入局部变量的想法。 我认为他们使用适当的解析器或动态创建新功能。

if (typeof loginError !== "undefined")

This allows you to check for variables that don't exist.

As mentioned by @GeoffChappel I've already adressed this earlier

I do change my mind of how they deal with injecting local variables though. I think they use a proper parser or dynamically create new functions.

更多推荐