madKC
madKC

Reputation: 55

EJS render parameters meaning

var express = require('express');

var app = express();
app.set('view engine','ejs');

var ejs = require('ejs'),
    people = ['geddy', 'neil', 'alex'],
    html = ejs.render('<%= people.join(", "); %>', {people: people});

app.get('/' , function(req, res){
    res.render("basic",{html});
});

app.listen( 3000,function() {
    console.log(html);
 });

the "basic" is a basic.ejs file:

<html>
<title>Hello</title>
<body>
<%- html %>
</body>
</html>

This works but I want to know what exactly is going on in this line

html = ejs.render('<%= people.join(", "); %>', {people: people});

What does the statement {people: people} do ?

Thank you

Upvotes: 2

Views: 8009

Answers (2)

Mahmoud
Mahmoud

Reputation: 571

In your case, you can simply replace this:

html = ejs.render('<%= people.join(", "); %>', {people: people});

with this:

html = people.join(", ");

The only difference is this

<%= Outputs the value into the template (HTML escaped)

Since your array is safe, you don't need to use <%=

Upvotes: 0

Barnab&#233; Monnot
Barnab&#233; Monnot

Reputation: 1163

{people: people} puts the array of people, so ['geddy', 'neil', 'alex'] in an object, associated to the key people. So when EJS renders <%= people.join(", "); %>, it goes to the object that you passed to the function ({people: people}), looks for a key named people and uses the value that is associated.

Upvotes: 2

Related Questions