I am working on a chatbot project using Handlebars in NodeJS with TypeScript. I need some guidance on how to pass an object (e.g. TransactionManager) to Handlebars helper functions without exposing it to the template. I don’t want to expose the functions nor the variables in the template.
Requirements
- For each session, I have a TransactionManager object that is unique
to that session. - This TransactionManager object needs to be accessible inside
Handlebars helpers when rendering the template, as it contains
session specific data and methods (e.g., runSomeFunction() or m_DatabaseConnection). - I don’t want the TransactionManager object to be accessible directly
in the template itself. So using
{{TransactionManager.runSomeFunction}} or {{TransactionManager.m_DatabaseConnection}}
or{{@TransactionManager}}
in
the template should not work. - The TransactionManager object is created dynamically at runtime when
rendering a template, so it isn’t available when I define the
Handlebars helper functions.
What I’ve tried:
I’ve considered adding the TransactionManager to the context or options.data, but this makes it accessible via {{@transactionManager}}
in the template, which I want to avoid.
Using closures in helper functions doesn’t seem feasible because I need to create the TransactionManager on a per session basis, meaning it must be added dynamically when rendering the template and I don’t want to create a Handlebars context for each session.
Ideal behavior:
I want to pass the TransactionManager object to the helper functions when rendering the template without exposing it to the template itself.
Helper functions should be able to access this object (e.g., this.TransactionManager.runSomeFunction()
), but the object with functions and variables should remain invisible to the template context.