質問

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!

役に立ちましたか?

解決

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.

他のヒント

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 %>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top