NakedWarrior NakedWarrior - 1 year ago 135
Node.js Question

How to fill data into ejs view and return a string is pure html with expressjs

I have a ejs view like this

<%= user.firstName %> <%= user.lastName %>

and in my controller I want to do something like this

var html = fill('ejs-template', data);

then I can use the html with data content to do something else

Answer Source

The quickest way, since you're using Express:

res.render('ejs-template', data, function(err, html) {
  if (err) return res.sendStatus(500); something with the HTML...

If you don't necessarily want to use Express, you can use the ejs module directly:

var ejs = require('ejs');
ejs.renderFile('./views/ejs-template.ejs', data, options, function(err, html) {

More info here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download