Search code examples
javascriptnpmhandlebars.js

Having multiple instances of a npm module


I'm currently implementing handlebars as a view engine. In order to serve different users with different helpers/partials I need multiple instances of Handlebars. Can anyone provide me an example of how to do this, because I can't figure out how to achieve this. Thanks in advance :)


Solution

  • From Handlebars.create():

    Creates an isolated Handlebars environment.

    var OtherHandlebars = Handlebars.create(); 
    

    Each environment has it's own helpers and partials. This is only necessary for use cases that demand distinct helpers or partials. Most use cases can use the root Handlebars environment directly.

    Templates created for a given environment are bound to that environment. This means that templates that need to run in multiple environments will need to be recompiled or reconstructed via Handlebars.template for each environment. This applies to partials as well.