How to include an EJS template in a string?

I want to pass my variables to this template, let it render, and then get the resulting HTML as a string.

How to do it in Express?

+7
source share
1 answer

Depending on the version of ejs, the following should work.

var ejs = require('ejs'), fs = require('fs'), file = fs.readFileSync(__dirname + '/template.ejs', 'ascii'), rendered = ejs.render(file, { locals: { items:[1,2,3] } }); console.log(rendered); 

You may need to install ejs if it is not already installed.

 cd;npm install ejs 
+20
source

All Articles