BackPacker777
BackPacker777

Reputation: 683

Is it possible to pass JSON object to ejs renderFile?

I'd rather not have to type out each field name of my ejs file. Here is what i'd like to do:

let html = null;
EJS.renderFile('./public/views/results.ejs', {JSON_OBJECT}, (err, str) => {
     html = str;
});

Upvotes: 4

Views: 723

Answers (1)

Andre T
Andre T

Reputation: 529

Something encapsulated in {} will create a new object.

So in

var JSON_OBJECT = {test: "value"};
var obj2 = {JSON_OBJECT}

obj2 will be an object with a property named JSON_OBJECT, which in itself has a property test.

EJS.renderFile accepts a JSON object as its second parameter. The properties the object can contain are described in the package description of ejs.

Upvotes: 5

Related Questions