开发者

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

开发者 https://www.devze.com 2023-03-07 15:09 出处:网络
Here is my scenario: I have a login.jade template where I authenticate users. Inside that template I have a few if\'s like:

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 r开发者_如何学Pythonendering 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")

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号