How to use Handlebar to reference System Variables
I am working on a POC where I want an email workflow to have a simple
{{{message}}}
variable. The value for message
will be generate on our side of the call. However, we want to use "System Variables" inside the "message" variable.
The first attachment should be a simple email template with "{{{message}}}" in the "Editor".
The second screen shot should contain the the "Preview" tab with "Hello {{actor.firstName}}" in the white panel.
The payload is:
Is it possible with something like custom handlebars or "custom code" to replace {{actor.firstName}}
with the system value?15 Replies
Hi @Todd H., I'm an AI assistant for Novu. While a team member reviews this question, I'll see if I can find relevant sources for your question.
-----------------------------
While I generate an answer, here are some sources that might be helpful:
* General
* Handlebars & Helpers
* Concepts
* NodeJS quickstart
-----------------------------
Answer
In Novu, you can utilize Handlebars syntax to dynamically insert system or user-defined variables into your notification content. For your use case, where you want to include a variable like
{{actor.firstName}}
within another variable {{{message}}}
, you'll need to ensure that the message variable itself is processed as a Handlebar template before being injected into the main template.
Here's how you can structure this:
1. Create a Handlebars Template: First, define your email template that includes the {{{message}}}
placeholder. This allows for HTML-safe rendering.
2. Pre-process the message
Variable: Before sending the payload to Novu, preprocess the message
variable on your server to replace {{actor.firstName}}
with the actual system value. This preprocessing should parse the message
variable as a Handlebar template.
3. Send the Preprocessed Message: After preprocessing, send the final message
string as a part of your API payload where message
contains the actual content to be displayed.
This approach requires handling part of the template processing on your server before sending it to Novu. Novu will then simply render the preprocessed message as it is. Ensure your server-side Handlebars processing safely handles user input to avoid injection vulnerabilities.
Unfortunately, direct references or nesting of Handlebars templates within variables processed by Novu isn't supported as per the current documentation. You would handle nested templating on your server-side or preprocessing step.
For more guidance on how to structure the templates and payloads in Novu, refer to the Handlebars and Helpers documentation (1) .Got it. Tagging @Support for additional help.
Step 2 - Pre-Process the Message is something we are considering. However we want to use Novu's System Variables.
@Todd H. would you mind sharing some more context? are you trying to access more data/code available on or backend? are you trying to generate a more complicated/require more business logic message?
Hi. We already have template logic to produce messages like, "<username>, welcome to Novu." Since the user's contact information is in Novu, we want that to be the source of truth when sending messages. We do not really need the template logic in Novu. We just want to pass in our own HTML formatted message, but get the system variables from Novu, like {{actor.firstName}}
Got it. Did you look at the Novu code first approach? Might be exactly what you need. https://docs.novu.co/echo/quickstart
Novu
Quick Start - Novu
Novu Echo, the Code-first notification workflow engine. Build advanced workflows while giving your non-technical teammates complete control over content and behaviour.
I was hoping to use the Workflow Editor in order to keep it simple for this POC. Are you stating that it is not possible to use the Workflow Editor?
Correct, in the Workflow Editor you can form the text, and use the variables, or you can pass a premade one. If helpful you can try and use an intermidate solution like:
So you can pass big chunks of the pure HTML as payload, and the rest you and your team members can control directly from the editor. Can that help?
In the Editor for the Email workflow I have a simple variable
{{{message}}}
.Then I want it issue a curl command such as:
I expect the
{{subscriber.firstName}}}
to be replaced back on the subscriberId
"barfoo-xyz". However, all I get is@Tomer Barnea Do you understand what I am trying to convey with my example?
Hey @Todd H. thats very interesting, as you saw, we don’t have it today, let me relay that to the team, and get back to you.
Is there no plan to allow other team members to change the content in production? Or is this just for POC purposes?
@Tomer Barnea Thank you.
It is for POC purposes for the time being. But I have no doubts this will quickly go into production once we nail down the requirements and get the proper dev support to implement it. We already have logic that has templated messages that we display to our customers in our app and reports. We also have our own translation service we use for the global community. I know Novu offers a lot of features. But for now we primarily want to use it more like "messaging-as-a-service".