문제

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