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

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 %>

Related

For loops in ejs

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

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.

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

Can you have multiple forms to the same url on a page?

I've got an MVC app that lists some products and I have a + and - graphic to increase/decrease the quantities. I surround these in form tags and have a couple of hidden fields one for the product id and one for the quantity. Both forms go to the same controller method. The only difference is the quantity 1 for increase -1 for decrease. The odd thing is the browser is only outputting 1 form field around the + (the second form on the page). Is it not possible to have the same form tag going to the same url on a page? I've even tried entering a unique id attribute, but to no avail.
As I type I've considered I could convert the graphic to an input of type image with the value as the quantity and wrap them both in the same post, not sure if that would work.
EDIT: to add code
<td style="text-align: right; width: 27px;">
<% using (Html.BeginForm("Update", "Cart", FormMethod.Post, new { id = "form-remove" }))
{ %>
<%= Html.Hidden("stockId", line.StockId)%>
<%= Html.Hidden("quantity", -1)%>
<%= Html.Hidden("returnUrl", ViewData["returnUrl"])%>
<input type="image" class="icon icon-down" src="/Content/Images/spacer.gif" />
<% } %>
</td>
<td style="text-align: right; width: 20px;">
<% using (Html.BeginForm("Update", "Cart", FormMethod.Post, new { id = "form-add" } ))
{ %>
<%= Html.Hidden("stockId", line.StockId)%>
<%= Html.Hidden("quantity", 1)%>
<%= Html.Hidden("returnUrl", ViewData["returnUrl"])%>
<input type="image" class="icon icon-up" src="/Content/Images/spacer.gif" />
<% } %>
</td>
<td style="text-align: right; width: 27px;">
<% using (Html.BeginForm("Remove", "Cart"))
{ %>
<%= Html.Hidden("stockId", line.StockId)%>
<%= Html.Hidden("returnUrl", ViewData["returnUrl"])%>
<input type="image" class="icon icon-remove" src="/Content/Images/spacer.gif" />
<% } %>
</td>
Above is an extract from my MVC view containing the icons. The update quantity by 1 renders but the -1 doesn't, even when adding the id attribute to the form. If I modify the action method to 'AddItem' and 'RemoveItem' instead of 'Update' it all renders fine, but then I'm almost duplicating my code with the only difference being the quantity parameter.
yes, you can have multiple forms on a page, here is a blog post on how to do it "asp net mvc handling multiple form actions on one view" another option is to use an ajax request like this "easy ajax with aspnet mvc and jquery"