I run Sails 0.9.7
and have installed Handlebars which is supported by Consolidate.js and therefore is supported by Sails
I can serve pages from .handlebars
files, it works just fine.
I can't figure where, in Sails workflow, and in a Sails way, I should register partials, helpers etc... I'm more looking for best practices than just a working solution but any help will be appreciated.
Sails supports handlebars and its (multiple-layout, partials) natively, if we use .handlebars extensions for our files instead of .hbs.
So to use handlebars in Sails instead of EJS, it advised to use consolidate(Its a template engine consolation library). Handlebars works good with SailsJs + consolidate.
You need to install consolidate.
And then you just have to update the config/views.js file with the following content.
Update all your .ebs files to .handlebar files and update the code inside it.
Everything will work fine.
There is a view-generator, for the later purpose which will generate default views for Sails(It will make a default directory structure with default files in it).
You can find it in the github repository.(https://github.com/bhaskarmelkani/sails-generate-views-hbs)
It is similar to the one officially launched by SailsJs for jade called balderdashy/sails-generate-views-jade.
I'm running out of time but I'm getting close to an answer, I think. I'll update this reply when I get more details, but if you want to poke at it, check out line 501 in the included consolidate.js file. View on github here: https://github.com/balderdashy/sails/blob/master/lib/configuration/consolidate.js#L501
It looks like for Handlebars there is a for loop that registers partials from options.partials.
That is not exactly a very satisfying solution, but if you push your partials on to that options object then maybe it will pull from that.
The big question I have next is, what is the options object, and where does it get set at?
For configuring handlebars template in sails js , follow below steps:
1) install handlebars in your application's node_modules folder:
2) change your config/views.js
I'm using the answers given above, but I seem to have made partials work with sails 0.9.8 with no hacks.
Here's what I have.
Config/views.js => engine: "handlebars"
views/home/index.handlebars => main file using the partial.
views/home/partials/partial.handlebars => partial being used.
Then as long as you use something like this, it works perfectly. res.view({ partials: { partial: 'partials/partial', footer: 'partials/footer' } })
The paths are relative to the template file called by default. So if you want different partials per controller you use partials/ and if you want global partials for all templates you use ../partials/
Obviously this is completely unto you as you need to specify every partial manually in the controller anyway.
I'm running v0.10 beta but this shouldn't affect how I got it working below:
config/views.js
config/routes.js
SiteController.js
views/site/index.handlebars
views/site/partials/head.handlebars
views/partials/tail.handlebars
OUTPUT
Edit: This was fairly easy to implement via the express3-handlebars module while changing none of the default Sails functionality with the exception of asking you to move your layout file into
views/layouts
. I've opened up a pull-request here (https://github.com/balderdashy/sails/pull/1075) if you would like to check it out.After a bit of digging in the sails source-code, it's fairly easy to bring in partials when you render your view.
When you call
res.view
in your controller actions, just pass in a partials object as part of your "locals" which contains a list of partials you'd like rendered.Helpers can be registered in a similar way, by attaching a key named
helpers
and passing in the functionality.It would be nice if there was a more formalized way to do this in the Sails core, but for now this should suffice for those who want to use handlebars instead of ejs while preserving some semblance of layouts.