简体   繁体   English

(不)将局部变量传递给Express中的Jade模板(node.js)

[英](not) passing a local variables to Jade templates in Express (node.js)

Here is my scenario: I have a login.jade template where I authenticate users. 这是我的场景:我有一个login.jade模板,我在其中验证用户身份。 Inside that template I have a few if's like: 在那个模板里面我有几个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.

In Express I ALWAYS have to declare ALL local variables that MIGHT be used in that Jade template. 在Express I中,我必须声明可能在该Jade模板中使用的所有局部变量。 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. 我的观点是,如果在Jade模板中我会有10个'if'语句包含一个局部变量而不是渲染我将不得不总是传递这10个变量即使我只会在那个特定情况下使用一个。

I just thought that if I do not pass any variable to Jade template that the result of "if(var)" would be just "false". 我只是想如果我没有将任何变量传递给Jade模板,那么“if(var)”的结果将只是“false”。

Any chances of working this around? 有没有机会在这周围工作?

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 正如@GeoffChappel所提到的,我早些时候就已经解决了这个问题

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. 我认为他们使用适当的解析器或动态创建新功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM