What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?

前端 未结 13 1987
一向
一向 2020-12-12 16:02

On the EJS github page, there is one and only one simple example: https://github.com/visionmedia/ejs

Example

<% if (user) { %>
    

&

13条回答
  •  天命终不由人
    2020-12-12 16:41

    What I do is just pass a default object I call 'data' = '' and pass it to all my ejs templates. If you need to pass real data to ejs template, add them as property of the 'data' object.

    This way, 'data' object is always defined and you never get undefined error message, even if property of 'data' exist in your ejs template but not in your node express route.

提交回复
热议问题