How would you check for undefined property in ejs for node.js?

后端 未结 4 1774
清歌不尽
清歌不尽 2020-12-13 23:53

What is the best way to check for an undefined property in an ejs template?

(I\'m using the node.js package by TJ Holowaychuk)

Example:

var t         


        
4条回答
  •  醉酒成梦
    2020-12-14 00:32

    Another way to test for a property is to reference it indirectly via the locals object. Using your example:

    var tpl = '<% if(locals.foo){ %>foo defined<% }else{ %>foo undefined<% } %>';
    console.log(ejs.render(tpl, { locals: { bar: "baz"} }));
    

提交回复
热议问题