On the EJS github page, there is one and only one simple example: https://github.com/visionmedia/ejs
Example
<% if (user) { %>
&
if you plan to use the same object often, you could use a function like this:
<% function userObj(obj){
//replace user with what you name your object
if(typeof user === 'object'){
let result = user;
if(obj){
obj = obj.split('.');
for(let i = 0; i < obj.length; i++){
if(obj[i] && obj[i].trim() !== '' && result[obj[i]]){
result = result[obj[i]];
}else{
result = false;
break;
}
}
}
return result;
}
return false;
} %>
usage:
<% if(userObj('name')){
<h2><%= userObj('name') %></h2>
} %>
I had same issue, and luckily, I found that there is also a short-circuit function in JS (I knew there was one in Ruby and some other languages).
On my server/controller side (this is from Node.js/Express):
return res.render('result', {survey_result : req.session.survey_result&&req.session.survey_result.survey });
See what I did there? The && which follows after a possibly undefined variable (i.e. request.session.survey_result
object, which might or might not have form data) is the short-circuit notation in JS. What it does is only evaluate the part that follows the && if the part to the left of the && is NOT undefined. It also does not throw an error when the left part IS undefined
. It just ignores it.
Now, in my template (remember that I passed the object req.session.survey_result_survey
object to my view as survey_result
) and then I rendered fields as:
<table>
<tr>
<td> Name:</td>
<td> <%=survey_result&&survey_result.name%></td>
</tr>
<tr>
<td> Dojo Location:</td>
<td> <%=survey_result&&survey_result.dojo_loc%></td>
</tr>
<tr>
<td> Favourite Language:</td>
<td> <%=survey_result&&survey_result.fave_lang%></td>
</tr>
I used short-circuit there also, just for safe-keeps.
When I tried it with previously suggested ways, just:
<% if (typeof survey_result !== undefined) { %>
... <!-- some js and/or html code here -->
<% } %>
Sometimes, it would still try to evaluate the properties within the IF statement...Maybe someone can offer an explanation as to why?
Also, I wanted to correct that undefined
needs to be without the single quotes, as I saw done in previous examples. Because the condition will never evaluate to true
, as you are comparing a String value 'undefined'
with a datatype undefined
.
Came to this page for an answer but I came up with a shorter inline syntax for it which is:
<h2><%= user.name ? property.escrow.emailAddress : '' %></h2>
<% if (locals.user) { %>
// Your logic goes here
<% } %>
Try prepending the variable with locals
Example: if(locals.user){}
You can create a view helper which checks for "obj === void 0", this one is for express.js:
res.locals.get = function() {
var args = Array.prototype.slice.call(arguments, 0);
var path = args[0].split('.');
var root = this;
for (var i = 0; i < path.length; i++) {
if(root[path[i]] === void 0) {
return args[1]?args[1]:null;
} else {
root = root[path[i]];
}
};
return root;
}
Then use it in the view like
<%- get('deep.nested.non.existent.value') %> //returns: null
<%- get('deep.nested.non.existent.value', "default value") %> //returns: default value