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

前端 未结 13 1979
一向
一向 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:39

    You can use this trick :

    user.name // stops code execution,if user is undefined
    (scope.user||0).name // === undefined
    

    where scope is parent object of user

提交回复
热议问题