VirtualWolf
VirtualWolf

Reputation: 723

How to configure Sails.js to use EJS view helpers?

I'm totally new to Sails.js and am trying to be able to use EJS's view helpers (mostly for link_to at the moment). I've seen references that it doesn't work out of the box, but I haven't seen any noob-friendy description of how to configure Sails.js to use the view helpers. Currently I have a totally bare-bones application generated with sails new <name> and not much else.

Thanks!

Upvotes: 2

Views: 3271

Answers (2)

Marcelo Boeira
Marcelo Boeira

Reputation: 880

Sails 0.10.x:

https://github.com/balderdashy/sails/issues/2162#issuecomment-55866731

config/http.js

module.exports.http = {
  // ...
  locals: {
    filters: {
      formatDate: function(date) { }
    }
  }
}

config/bootstrap.js

_.extend(sails.hooks.http.app.locals, sails.config.http.locals);
views/test.ejs

At some view...

<%=: created | formatDate %>

Upvotes: 2

Phil Misiowiec
Phil Misiowiec

Reputation: 251

The EJS npm module that comes with Sails does not include helpers, which may not be immediately apparent since the Sails.js views documentation links directly to http://embeddedjs.com/. So you must first install the 'express helpers' npm package:

npm install express-helpers --save

Then, inside your app's config/bootstrap.js, add this inside the bootstrap function:

require('express-helpers')(sails.express.app);

Restart your app and your view template should now properly render any link_to's.

Upvotes: 6

Related Questions