EJS: Pass array of pages into include(page) - node.js

I've created a dashboard page where a user can save different components of the site to one page for quick viewing. I'm able to dynamically load one component as follows:
index.js
res.render('dashboard',{comp: 'component1'});
dashboard.ejs
<%- include(comp) %>
but I would like to do something like this:
index.js
res.render('dashboard',{comp: ['component1', 'component3']});
And have the ejs page loop through the include(), this way i can show 0 to n components on the dashboard page.
I've tried wrapping the include in a for loop like so:
<%- for(c in comp){include(c)} %>
but ejs did not like this.
Am I going about this the wrong way?

Try this
<% for(var i=0; i < comp.length; ++i) { %>
<%- include(comp[i]) %>
<% } %>

in your code, c is the index not value, comp[c] is your component.
<% for(c in comp){ %>
<%- include(comp[c]) %>
<% } %>

Related

ejs array in an object

Here is the code in nodejs(express):
response.redirect('file',{test: [{name:'sarah',arr:[1,2,3]},{name:'beck',arr: [2,3,4]}]
Now I want to access the 'arr' for every name in the array of objects.In ejs file:
<% test.forEach(index,item){ %>
<p><%= item.arr %></p>
<% }) %>
This is printed as 1,2,3 and 2,3,4 but I want the answer to be [1,2,3] and [2,3,4].Can someone help?
You can use a for .. of loop to iterate over all entries of test and then use JSON.stringify on the entries' arr property:
<% for(const testData of test){ %>
<%= JSON.stringify(testData.arr) %>
<% } %>

ejs + nodejs - compare two variables doesn't work

I pass two variables during rendering in nodejs. Let's say they're templates & treeInfo.
In template.ejs I have.
<% for(var i=0; i<templates.length; i++) {%>
<%= templates[i]._id %> = <%= treeInfo.owner[0] %><br>
<% if (templates[i]._id == treeInfo.owner) { %>
ok
<% } %>
<% } %>
So actually if == doesn't work as expected. Here's the output.
59519779f36d284c166f9bea = 5941789e36593262bed9256b
5941789e36593262bed9256b = 5941789e36593262bed9256b
So it doesn't compare them the right way. If I just replace treeInfo.owner with something like '5941789e36593262bed9256b', it does work fine.
I assume ejs doesn't support variables comparation?
Thanks
Variable type can't understand.That's why toString() is required.
<% for(var i=0; i<templates.length; i++) {%>
<%= templates[i]._id %> = <%= treeInfo.owner[0] %><br>
<% if (templates[i]._id.toString() == treeInfo.owner.toString()) { %>
__append('Hello World');
<% } %>
To compare two objects / variables in EJS, as well as in nodejs
if(user1.equals(user2))
{
console.log("Both are equal");
}

How to place an EJS tag within another

I am using EJS as my templating engine. I am passing variables from node js to ejs like...
router.get("/AdminDatabase", function(req, res) {
res.render('Pages/AdminDatabase', {title: 'WebFormsAdmin', role: 'System Admin' });
});
I am building a role base control and for this I want to change the header of the page base on the role of user.
<% include ../partials/Common/header_<%= role %> %>
The problem is with the above segment. How can I place the variable role inside this EJS code segment?
My header files are
header_System Admin.ejs,
header_Survey Admin.ejs,
header_Survey Taker.ejs
A workaround would be to do a conditional render like so:
<% switch (role) {
case 'System Admin': %>
<% include ./partials/header_System_Admin %>
<% break; %>
<% case 'Survey Admin': %>
<% include ./partials/header_Survey_Admin %>
<% break; %>
<% default: %>
<% include ./partials/header_Survey_Taker %>
<% break; %>
<% } %>
Note that the first case must be grouped with the switch declaration. Make sure the paths are correct for your partials.
You can concatenate the path and the variable.
<%- include('../partials/Common/header_'+role) %>

ejs - "Unexpected identifier" when using include in for loop

I'm using <% include components/aside.ejs %> or <% include components/head.ejs %> somewhere in my code without any problem. But when I use include in a for loop like this
<%
for (var i = 0; i < 20; i++) {
include components/head.ejs;
}
%>
, I get Unexpected identifier in [file path] while compiling ejs.
Is there any obvious mistake that I'm not noticing?
To fix a breaking change, as of EJS 3.x, the syntax for an include has gone from <%- include components/head.ejs %> to <%- include('components/head.ejs'); %>.
You can try this one.
<% for (var i = 0; i < 20; i++){ %>
<%- include('component/footer') %>
<% }; %>
Include the template tags <% and %> on every line, like this:
<% for (var i = 0; i < 20; i++){ %>
<%- include components/head.ejs %>
<% }; %>

How to get all locals in Express view

I am trying to figure out a way to get access to all the app.locals in the views. In my app.js I have
app.locals.title = "Title"
app.locals.page = "Page 1"
This is good, and in the views, I can access each with
<%= title %>
<%= page %>
However, is there a way (without doing something like app.locals.xyz.title) to get all the locals in the view so I can do something like this:
<% for(var i=0; i < locals.length; i ++ ){ %>
<li><%= locals[i] %></li>
<% } %>
You have to explitly pass it:
res.locals.locals = res.locals;
Then in the view:
<%= locals %>

Resources