Questions tagged [ejs]

"E" is for "embedded." EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. No religiousness about how to organize things. No reinvention of iteration and control-flow. It's just plain JavaScript.

Embedded JavaScript (EJS) is a set of two open source libraries providing in-browser client side templates for web development and as a template system for node.js (including client-side template functionality). EJS uses <% %> or [% %] tags, executing any JavaScript within the tags. Adding an equals sign (<%= %>) causes the enclosed JavaScript to be evaluated, and the toString representation to be appended to the document.

The original client-side EJS has been subsumed into JavaScriptMVC, official support is offered at their forum (the old Google Groups group is no longer active).

Install via npm: npm install ejs.

References:

6440 questions
50
votes
4 answers

how to include a template with parameters in EJS?

As a real beginner in EJS, I have two charts in my html page, so I want to use my partial twice: <% include partials/spider-chart.ejs %> But I need to pass some parameters inside the ejs to differentiate between graphs. What is the best way?
Orkun
  • 6,998
  • 8
  • 56
  • 103
49
votes
4 answers

How would you check for undefined property in ejs for node.js?

What is the best way to check for an undefined property in an ejs template? (I'm using the node.js package by TJ Holowaychuk) Example: var tpl = '<% if (foo) { %>foo defined<% } else { %>foo undefined<% } %>'; console.log(ejs.render(tpl, { locals: {…
pvorb
  • 7,157
  • 7
  • 47
  • 74
49
votes
8 answers

Pass variables to JavaScript in ExpressJS

I am completely lost on this; I am using NodeJS to fetch a JSON and I need to pass the variable to my page and have JavaScript use the data. app.get('/test', function(req, res) { res.render('testPage', { myVar: 'My Data' }); That is…
Jetson John
  • 3,759
  • 8
  • 39
  • 53
48
votes
6 answers

Syntax highlight for .ejs files in vim

What is the best way to make vim highlight ejs (http://embeddedjs.com/) files? Is it possible to set up html highlight for the file in general and javascript highlight to it's parts inside <% %>? Appreciate your help!
lyuba
  • 6,250
  • 7
  • 27
  • 37
45
votes
1 answer

How to escape HTML in node.js EJS view?

I want to escape the html in bloglist[i].Text field. How to do that with EJS? <%= title %>

<%= title %>

marko
  • 10,684
  • 17
  • 71
  • 92
39
votes
4 answers

Chart.js creating a line graph using dates

I can't seem to get Chart.js to work with dates. I have tried quite a few different methods: let examChart = document.getElementById("examChart").getContext("2d"); let examLineChart = new Chart(examChart, { type: "line", data: [ …
Jonathan Woollett-light
  • 2,813
  • 5
  • 30
  • 58
39
votes
5 answers

Passing an object to client in node/express + ejs?

I have a pretty large object that I need to pass to a function in a client script. I have tried using JSON.stringify, but have run into a few issues with this approach - mostly performance related. Is it possible to do something like this in…
Errol Fitzgerald
  • 2,978
  • 2
  • 26
  • 34
32
votes
7 answers

Where should I define JS function to call in EJS template

I am working on a template where I am trying to render template using express and ejs. As to the standard structure of node app, I have app.js file which which contains functions like following: app.locals.getFlag = function(country) { var…
LearningAsIGo
  • 1,240
  • 2
  • 11
  • 23
32
votes
6 answers

Can a js script get a variable written in a EJS context/page within the same file

As I wrote in the title, I'd like to get a value from a variable written into a ejs page/file, from a javascript file within the same page EJS: <% var test = 101; %> JS: Or what if I'd like to use a…
Donovant
  • 3,091
  • 8
  • 40
  • 68
31
votes
4 answers

Nodejs EJS helper functions?

Is there a way to register helper functions to EJS templates, so that they can be called from any EJS template? So, it should work something like this. app.js ejs.helpers.sayHi = function(name) { return 'Hello ' + name; }); index.ejs <%=…
Farzher
  • 13,934
  • 21
  • 69
  • 100
30
votes
5 answers

How can I comment the EJS code (JS node) without getting an error

I have this code in an EJS file: <% for(var i=0; i < data.length; i++) { %> <% } %>
<%= data[i].id %> <%= data[i].name %>
When I comment it this way,