user2673097 user2673097 - 1 month ago 8
Node.js Question

How to use else statement in jade template?

New to node.js. I've generated a simple website using express. I've added the following text in

index.jade
:

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


When I commented out the last two statements, I get the anticipated response: "It is raining. Take an umbrella!"

When I include the last two lines, I get an unexpected token error:

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)


Not sure why the inclusion of the else statement could cause an error. Any suggestions would be greatly appreciated.

Answer

be careful with indentation:

here is an example

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

check it jade reference

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!

or even:

| #{raining ? raining : 'man'}
Comments