For loops in ejs - html

I have a array containing some html content like this.
const articleArray=['<p>first text</p>\r\n','<p>second text></p>\r\n','<p>third text</p>\r\n']
I need to render this in an ejs file through a get request
request.render('viewArticles',{array : articleArray})
where 'viewArticles' is an ejs file. I have used the middleware to set the view engine as ejs.
This is the code in ejs
<% for(arr in array) {%>
arr
<% } %>
I am not able to render any html. How to solve this?

'<%- %>' this is help to print html code as it is.
<% 'Scriptlet' tag, for control-flow, no output
<%_ ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it
<%= Outputs the value into the template (HTML escaped)
<%- Outputs the unescaped value into the template
<%# Comment tag, no execution, no output
<%% Outputs a literal '<%'
%> Plain ending tag
-%> Trim-mode ('newline slurp') tag, trims following newline
_%> ‘Whitespace Slurping’ ending tag, removes all whitespace after it
for refrence please click visit this site EJS
<% array.forEach(function(item) {%>
<%-item%>
<% }) %>

Try this
<% array.forEach(function(item,index){ %>
<%= item %>
<% }) %>

I'm not really sure how to do what you want exactly , but take a look at this:
First, instead of you creating an array of HTML elements, how about you create an array of objects, like so :
const articles = [{text: "first text"} , {text: "second text"}, {text: "third text"}];
this way you just have to write the HTML once, and I am fairly new to programming but I don't see a scenario where you would get a response in HTML, usually you query a database for data or from a JSON file... and assuming the array is actually getting passed to the .ejs file, lets iterate though it,
<% for(let a of articles){ %>
<p> <%= a.text %> </p>
</br>
<% } %>
If the array is NOT getting passed, check for these in your code :
// If you installed
..."dependencies" : {
"ejs": "x.x.x",
} //Check the package.json file
// If you imported correctly
import ejs = require("ejs");
// If you specified the views path
app.set("views", path.join(__dirname, "views"));
// And the views engine
app.set("view engine", "ejs");
// And your route
request.render('viewArticles',{ articles : articles }) // Check if the file name is correct, I get this wrong many times

Related

href links are not detected in a index.ejs file [duplicate]

This question already has an answer here:
How to escape HTML in node.js EJS view?
(1 answer)
Closed 1 year ago.
I have a nodeJS-HTML application. The html file accepts a form and calls nodeJS function which processes some data and send back some info to the hrml file. I am using views to render the details that need to be posted.
My app.js has this line of code
res.render('index.ejs', { collection: data})
where data is in the JSON format
"Name": "Chegg",
"Description" : "In February 2018, the diet and exercise service ABC suffered a loss.
My index.ejs looks like this
<% for(var i=0; i<collection.length; i++) {%>
<div>
<p align="left"><%= collection[i].Domain %> </p> <br>
<p align="left"><%= collection[i].Description %> </p> <br>
<!-- <td><%= collection[i] %></td> -->
</div>
<% }
%>
Here everything is populated correctly expect the Description. The links are not hyperlinked - but plain text.
Is there a way I can show the hyperlinks?
As mentioned by #dimitristseggenes hyperlinks works with - but not with =
Change
<%= collection[i].Description %>
To
<%- collection[i].Description %>

Can't access deeply nested objects and arrays in EJS template

Hello fellow stackers,
I trying to get the following response to my EJS template.
Currently I have a for-loop which iterates all campaigns.
campaigns.facebook.data[i].insights .data
Just tried this, which works fully and the output is being shown below:
<% switch (campaigns.facebook.data[i].status) {
case 'ACTIVE' : %>
<div class="ui grid">
<p><%- JSON.stringify(campaigns.facebook.data[i].insights) %></p>
...
However, while trying to get the .data object I encounter problems
<p><%- JSON.stringify(campaigns.facebook.data[i].insights.data) %></p>
Even while trying JSON.stringify(campaigns.facebook.data[i].insights.data[0]) doesn't give any luck. What is possibly wrong here?
After adding a conditional statement for checking if the desired object exist, we won't encounter the undefined error anymore.
<% if (campaigns.facebook.data[i].insights) { %>
<p><%- JSON.stringify(campaigns.facebook.data[i].insights.data[0]) %></p>
<%console.log(campaigns.facebook.data[i].insights.data[0])%>
% } %>

sending var in html from view backbone

Hi I am learning backbone.js and as a part of a project I want to send a variable from my view.js file in Backbone to the associated template(.html). How should i do it?
Currently I try to do the following but fail:
In View.js:
$( ".result" ).html( displayChoice );
In html template:
<% if(!displayChoice.localeCompare("true")) { %>
<div class="name" id="choices"><%- choices[i].choice %></div>
<% } %>
Please tell me what is wrong in this approach.

how I can read a from taglib to <% ... %>

could you help please?, how I can read the variable objd in a <% ... %> Assigned in the foreach?
<c:forEach var="objd" items="${beanDreqproducto}">
<%
Gson j = new Gson();
String data = j.toJson(objd);
%>
console.log("${objd.cantidad}, data: "+'${data}');
</c:forEach>
The ${} basically translates to PageContext#findAttribute(). So, this should do:
<%
Object objd = pageContext.findAttribute("objd");
// ...
%>
Note that you should set data in page scope as well in order to get ${data} to work:
<%
// ...
pageContext.setAttribute("data", data);
%>
See also:
Our EL wiki page
Unrelated to the concrete question, this approach is ultimately bad. You should choose to use either JSTL/EL or scriptlets exclusively, not both. If you prefer JSTL/EL, just create a custom EL function to do the desired job, so that you can end up something like as follows:
<c:forEach var="objd" items="${beanDreqproducto}">
console.log("${objd.cantidad}, data: ${my:toJson(objd)}");
</c:forEach>
Our EL wiki page contains a section how to create EL functions.

Rails: Adding an empty tag plus content to link_to

I'm trying to generate a link using the link_to helper that will output the following HTML:
<i class="some_class"></i>Link Name
However the code I'm using to try to accomplish this:
link_to(tag("i", class: options[:icon]) + title, url)
...is outputting:
<i class="some_class">Link Name</i>
Why is it doing this, and how can I fix it? Thanks.
EDIT:
I believe I found the issue.
<i> tags are not self-closable tags in HTML5. Therefore the text after the i is treated as that element's content.
Have you tried using the block format of link_to?
<%= link_to url do %>
<%= tag("i", class: options[:icon]) %>
Link Name
<% end %>
Tweak that to your needs and maybe you'll get what you're looking for.
This is the icon tag helper I use in my applications which I frequently pass as the first argument to link_to, which can either be used to create a icon tag alone, or an icon tag followed by text.
def icon_tag(icon, *args)
options = args.extract_options!
text = args.first || options.delete(:text)
if text.nil?
content_tag :i, "", class: ["icon", "icon-#{icon}"] + options[:class].to_a
else
"#{icon_tag icon} #{text}".html_safe
end
end