如何在玉模板中使用else语句?

时间:2016-10-21 20:59:40

标签: node.js express

node.js的新手。我用express创建了一个简单的网站。我在index.jade中添加了以下文字:

raining = true
if(raining === true)    
    p It is raining. Take an umbrella!
else  
    p No rain. Take the bike!

当我评论最后两个陈述时,我得到了预期的回应:“下雨了。撑伞!”

当我包含最后两行时,我收到意外的令牌错误:

Unexpected token (57:0)

SyntaxError: Unexpected token (57:0)
at Parser.pp.raise (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:943:13)
at Parser.pp.unexpected (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:1503:8)
at Parser.pp.parseExprAtom (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:327:12)
at Parser.pp.parseExprSubscripts (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:216:19)
at Parser.pp.parseMaybeUnary (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:197:19)
at Parser.pp.parseExprOps (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:151:19)
at Parser.pp.parseMaybeConditional (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:133:19)
at Parser.pp.parseMaybeAssign (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:110:19)
at Parser.pp.parseExpression (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:86:19)
at Parser.pp.parseStatement (/home/ubuntu/workspace/node_modules/jade/node_modules/with/node_modules/acorn-globals/node_modules/acorn/dist/acorn.js:1750:23)

不确定为什么包含else语句可能会导致错误。任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

小心缩进:

这是一个例子

http://jsfiddle.net/heanfig/z8wn5qz9/

检查玉器参考

http://learnjade.com/tour/conditionals/

- var raining = true
if raining == true
        p It is raining. Take an umbrella!
else
        p No rain. Take the bike!

甚至:

| #{raining ? raining : 'man'}