I am trying to display the value in array using _.each and EJS dynamically. I am getting the values when i do it manually,but getting Unexpected token ; error while using _.each loop.
Here is the code
`<% _.each(pro,function(prof) { %>
alert(prof.Name);
<% } %>`
pro = {[object Object],[object Object]}
when i do it manually,i get the output.
i.e.
`<%= pro[0].Name %>`
Need help! Any other suggestions? Thanks
You seem have missed a parentheses here:
<% }) %>`
Beyond that, I'm not sure you can use underscore inside your EJS templates.
Try using a regular for.
<% for(var i=0; i<prof.length; i++) {%>
<li><%= prof[i] %></li>
<% } %>
Related
I am working on this: https://www.youtube.com/watch?v=6FOq4cUdH8k
For some reason, the following line is causing the error in the title.
<% include ./partials/messages %>
Removing the line above solves the problem.
I have confirmed that it is not the messages file as there are no slashes in it.
<% if(typeof errors != 'undefined') { %>
<% errors.forEach(function(error){ %>
<%= error.msg %>
<% }); %>
<% } %>
as you can read in the official documentation
Includes are relative to the template with the include call. (This
requires the 'filename' option.) For example if you have
"./views/users.ejs" and "./views/user/show.ejs" you would use <%-
include('user/show'); %>.
You'll likely want to use the raw output tag (<%-) with your include
to avoid double-escaping the HTML output.
so instead of
<% include ./partials/messages %>
Write
<%- include ("./partials/messages") %>
Why?
Template tag <%- outputs the unescaped value into the template, whereas <% 'scriptlet' tag is used for control-flow, no output. The parenthesis and " " are used for filename location
I want to create an app where users have the ability to assign tags to their content using Node and MongoDB.
So far, I have been able to insert the tags into the database as an array.
This is how it looks like:
"tags": [
"laughing",
"smile"
]
Now I have a problem when it comes to displaying the tags on the frontend using the ejs template.
When I run the following the code:
<% for(var i=0; i<tags.length; i++) { %>
<%= tags[i] %>
<%}%>
The result is the following:
laughing, smiling
Yes it does give me the right answer but what I really wanted is a way to wrap each entity around <a></a>. something like smiling
I want to make this possible but I have not seen a way to achieve this using ejs.
Have you simply try:
<% for(var i=0; i<tags.length; i++) { %>
<%= tags[i] %>
<%}%>
Never used ejs but it look like all others templates languages syntax.
Try This -
<%var data = { "tags" : ["laughing", "smile"] };
var tagsArr = [];
for (let i = 0; i < data.tags.length; i++) {
tagsArr.push(data.tags[i].split(","));
}%>
Then use tagsArr -
<% for(let i=0; i<tagsArr.length; i++) { %>
<%= tagsArr[i] %>
<%}%>
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]) %>
<% } %>
In the following code if the semicolon is removed the template engine does not throw error, else the error is thrown.
<ul>
<% for (i = 0; i < array.length; ++i) { %>
<%= JSON.stringify(array[i]); %>
<% } %>
</ul>
Although putting semicolon syntactically correct, why does the template engine throw error ?
As #torazaburo said, the contents between <=% and %> are an expression that gets parsed by ejs. However, think of it this way:
Your goal is to render the contents returned from JSON.stringify to the page right? So in that case, think of ejs expressions as implicitly calling .toString() on the result of the expression and then inserting that string into the page. By adding a semi-colon you've terminated the statement without assigning the returned value to anything.
I'm using Node.js with Express web framework (and EJS template engine).
When I have to print a variable I do something like:
<% if (value) { %>
<%= value %>
<% } %>
Can I do the same thing without open others brackets ? Like:
<% if (value) { PRINT VALUE } %>
Is this possible? How to print the variable?
I'm amazed to find that apparrently you can't do it, like in PHP:
<?php if ($value) : ?>
<?php echo $value; ?>
<?php endif; ?>
However a slightly better solution may be to do
<%= (value) ? value : '' %>
I say this assuming that the condition may occasionally be more complex, i.e.
<%= (str.length > 100) ? truncate(str) : str; %>
Which is much nicer than
<% if (str.length > 100) { %>
<%= truncate(str) %>
<% } %>
even if it is a slightly contrived example.
I'd love to be shown a direct command to do it, as per your original question.
There is now an outputFunctionName parameter that you can use. According to the documentation:
outputFunctionName Set to a string (e.g., 'echo' or 'print') for a function to print output inside scriptlet tags.
<% console.log(posts) %>
NB: Make sure you define your variable in any other file you have eg app.js file...
let posts = [];
app.get("/", (req, res) => {
res.render("home", {
posts: posts
});
});