I’m simply trying to display a value in an input field with Jade (0.20.3) and Express (2.5.8):
input(name='username', type='text', id="username", value=username)
It is quite simple, but this throws an error when the value is undefined:
username is not defined
However, the documentation indicates:
When a value is undefined or null the attribute is not added, so this is fine, it will not compile 'something="null"'.
Is there something that I would have done wrong?
Short answer: use
locals.someVarif you’re not sure thatsomeVarexists.Longer Answer:
I think that Brandon’s initial answer and last comment are correct (though the
#{...}syntax isn’t needed), but to elaborate a bit: There’s a difference between passing in an a variable (technically, an object property) with a value ofundefined, and not passing that variable at all.Because Jade transforms your template into JS source and
evals it (in the context of awithblock), you have to make sure that you’re not referring to any variables that haven’t been passed in, or they’ll be . This blog post has some background on undefined vs undeclared variables andReferenceError.Your Jade template should work correctly if you do one of these things:
But these won’t work:
If it’s not practical to manually pass in each parameter you might want to refer to, you can refer to the variable in your template as properties on the
localsobject (e.g.locals.username)