Daniel Marín Daniel Marín - 1 year ago 121
Javascript Question

ejs load template from a file

I just found this library called ejs, and I want to use it. According to the examples in the documentation I am doing the following:

var Ejs = require('ejs')

var loadTemplate = function () {
var users = ['geddy', 'neil', 'alex'];
var html = Ejs.render('<%= users.join(" | "); %>', {users: users});

Which is working fine, but what I want to do is to load the template from a file.. something like this:

var template = Ejs.render('url:/template.ejs', {users: users});

I am not being able to find any example of this simple case, any idea of how I can do this? without having to use fs.

Answer Source

From the tests, it looks like the renderFile fuction is what you want:

  var ejs = require('ejs')

  it('should accept locals', function(done){
    var options = { name: 'tj', open: '{', close: '}' };
    ejs.renderFile('test/fixtures/user.ejs', options, function(err, html){
      if (err) return done(err);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download