I'm using EJS with a Node.js web server I'm building. I see many EJS examples that sometimes use <%= when outputting HTML or strings, while other examples (sometimes within the same template) use <%-.
I tried to reference the EJS docs and getting started guide, but both gave no info about the <%- notation. Also, my Google search attempts yielded nothing useful. Thanks!
The version of EJS you're likely using in Node is not the same as the version you see on Google code; in the Node version, <%= escapes the HTML going into the buffer, while <%- does not. source
From http://ejs.co/:
<% 'Scriptlet' tag, for control-flow, no output
<%= Outputs the value into the template (HTML escaped)
<%- Outputs the unescaped value into the template
<%= *param* %> is use for transfer data from view to controller and vice versa
while <%- %> is to include other code
For my project is
With <%= you if would render some variables that holds a string that holds HTML code, it would not render that HTML code but render it as text to avoid cross-site scripting attacks.
With a minus ( <%- ) you can avoid this, and really render the HTML code.
1)Option One: an ejs file is importing a variable called "noob" which holds for example a req.body value.
<p><%= noob %></p>
2)Option two: an ejs file importing a partial template called "myPartial.ejs" which is inside "partials" folder.
<p><%- include("./partials/myPartial") %></p>
3)Option three: an ejs file is using javascript code:
<p>
<% while (i < 10) { %>
myVar += "The number is " + i;
i++;
<% } %>
</p>
Related
I am trying to write an ejs file with a for-each loop that displays, as text, names of javascript variables line by line. I want each name to be hyperlinked to a route, such as /getinfo, that renders a page that displays more information about the variable that was clicked on.
I tried to use an "a href" tag as follows:
<a href="/getinfo?name=" <%= variable.name>> <%= variable.name %></a>
I expected variable.name to be included in the URL's query parameters, but it won't be appended properly. Considering how HTML does not support string concatenation, I am not sure how to properly include this data in an HTTP GET request.
What would be a correct way to achieve this? Thank you!
It's simple. Using template literal
<a href="<%= `/getinfo?name=${variable.name}${variable.name}` %>">
Regular ejs to output escaped html
<a href="/getinfo?name=<%= variable.name %><%= variable.name %>">
I am rendering a view with the EJS template engine based on some dynamic variables. For example:
res.render("index", {
dynamicVariable: newEverytime
});
I am currently inserting it inside my HTML like so
<% if(someCondition){ %>
<div>
<%= dynamicVariable %>
</div>
<% } %>
I want EJS to create a new <a>tag every time as opposed to replacing my a tag each time. How can I achieve this?
EJS on the server side doesn't create any DOM nodes. It processes HTML source code as text. It just prints strings and it's the HTML parser on the client side that creates the DOM nodes from that.
I think you are asking about a strange solution to a problems that you say nothing about. It would b more useful to ask about the actual problems you're having, what have you tried so far and why it's not working.
In Ruby on Rails, there is a block called <% provide:titleName %> and <% yield:titleName %> that can be used to switch out text when rendering from page to page.
Is there an equivalent method in Sails.js that mimics this?
Yes there is if you use the standard ejs templating
IN the HTML file use:
<%= variablename %> //This will be replaced by the data in variablename
In the Controller method used to serve the view use:
res.view("viewname", {variablename: someValue});
I hope this is what you are looking for, let me know!
I am using node.js and express.j s to render an index.jade page which contains several script blocks taht contain templates to be used via backbone and underscore. The issue I am facing is that due to the inclusiong of <%= %> style variables within the templates, the Jade rendering is failing. The following code snippet causes a syntax error:
script#tpl-things-list-item(type='text/template')
td
a(href=<%= _id %>) link text
td <%= name %>
td <%= age %>
Note that it is only a problem when I use a variable inside the href value, if I remove the entire href, this snippet works just fine. Is there a way to work around this? I'd like to continue using Jade to define the templates as it is very concise but this is a show stopper.
Got it.
!!! 5
html(lang='en')
head
title= title
body
h1= "Hello World!"
script#tpl-things-list-item(type='text/template')
td
a(href!="<%= _id %>") link text
td <%= name %>
td <%= age %>
I'm using express.js and EJS as template engine.
I don't understand how to use partials, I have seen at the examples but the author used JADE template engine, so I don't know how to apply it with EJS.
I have a simple view named: test.ejs and another .ejs file named part1.ejs
I need to show part1.ejs inside test.ejs.
I tried putting <% partial('part1', {}) %> (into test.ejs) but nothing happen, It does not include that file.
Could someone give me an example?
Thank you!
The correct code in your situation would be:
<%- partial('part1') %>
If you want to include unescaped HTML use <%- and if you want to escape HTML (unlinkely though when including a partial) you can use <%=.
Resources:
Node.js - EJS - including a partial
http://groups.google.com/group/express-js/browse_thread/thread/62d02af36c83b1cf
Its an old thread, but here is how you do it in the newer version of EJS.
<% include part1 %>
given part1.ejs contains the html you wish to include.