Why do we need `onCompile` part when registering `express-hbs` engine

#1

Here’s how Ghost registers express-hbs engine:

instance.configure = function configure(partialsPath) {
    var hbsOptions = {
        partialsDir: [config.get('paths').helperTemplates],
        onCompile: function onCompile(exhbs, source) {                      <==================== ???
            return exhbs.handlebars.compile(source, {preventIndent: true});
        }
    };
   ...
};

Can anybody please tell me why this onCompile part is needed? Is it really just to pass {preventIndent: true} option to the compiler. In that case I’m wondering why there’s no way to pass it to engineInstance.express4()?

#2

Overriding onCompile is how you pass options to the underlying handlebars compiler when using express-hbs.

express-hbs has settings but they are all related to it’s own behaviour not the behaviour of the external handlebars dependency. The onCompile hook is the library’s method to define how you want compilation to happen, as you can see in the example in their docs you may only want preventIndent to be passed for certain files, that would be much harder to achieve with a generic setting compared to the onCompile hook.

#3

@Kevin thanks a lot for your reply! Interesting, I tried to figure out if express-hbs has it’s own compilation process or simply adds a few options to handlebars.js compiler. The latter seem to be the case. The call to handlebars.compile returns this function and express-hbs simply calls it in renderTemplate passing extra templateOptions.