How to configure partials and layouts for Handlebars in Sails.js?

后端 未结 7 960
暖寄归人
暖寄归人 2020-12-25 08:27

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

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-25 09:19

    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.

    npm install consolidate --save
    

    And then you just have to update the config/views.js file with the following content.

    module.exports.views = {
    
    engine: {
        ext: 'handlebars',
        fn: require("consolidate").handlebars
      },
    
    layout: 'layouts/layout',
    
    partials: 'partials/'
    
    };
    

    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.

提交回复
热议问题