ejs and null

688 views
Skip to first unread message

Angelo Chen

unread,
Feb 8, 2012, 9:37:11 AM2/8/12
to Express
hi,

using ejs in expressjs app, if a property of json is null, used in a
form like following, the field shows 'null', is there a way not
showing as 'null' but just a blank?

<input type="text" name="photo[remark]" value="<%=photo.remark%>" />

thanks,

angelo

Dmitriy Petrov

unread,
Feb 8, 2012, 10:52:14 AM2/8/12
to expre...@googlegroups.com
Hi,

I think You can use this

<input type="text" name="photo[remark]" value="<%=photo.remark || ''%>" />

Angelo Chen

unread,
Feb 8, 2012, 8:12:05 PM2/8/12
to Express
Hi,

very clever! but why it works? || is logical operator of js, can not
figure out why.

angelo

Dmitriy Petrov

unread,
Feb 9, 2012, 4:51:54 AM2/9/12
to expre...@googlegroups.com
Hi,

You can use javascript between <% %>, thats why it works
http://nodejs.org/docs/latest/api/vm.html

Pablo Botelho

unread,
Feb 9, 2012, 12:55:34 PM2/9/12
to expre...@googlegroups.com
but why the logical operator OR prevents the 'null'  text from being shown?

2012/2/9 Dmitriy Petrov <i.am....@gmail.com>


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.


Paul Vencill

unread,
Feb 9, 2012, 1:46:57 PM2/9/12
to expre...@googlegroups.com
Because of how JavaScript logic works.  A null interprets as a logical 'false' also, so you can use || as a null coalescing operator as well as a boolean operator.  It's common to do things like:

function my_func(options){
    options = options || {};  // where {} might also be the default options
 /*... more code .. */
}

To unsubscribe from this group, send email to express-js+...@googlegroups.com.

Pablo Botelho

unread,
Feb 9, 2012, 5:22:18 PM2/9/12
to expre...@googlegroups.com
thank you very much!

2012/2/9 Paul Vencill <paul.v...@gmail.com>
Reply all
Reply to author
Forward
0 new messages