N
Novu•3y ago
Kristof

Push notification templating

Hi, I was wondering if push notification title and body also support some form of templating such as handlebars or JS? I would like to pass an array of names for example and render it as {{ names.length === 1 ? names[0] : names[1] + ' and ' + names[1]}}. Would be nice to have this logic in Novu.
18 Replies
Pawan Jain
Pawan Jain•3y ago
Is it not working now?
serafeimo
serafeimo•3y ago
Hi @kristofdt I don't know if it helps but you can add place holders in the template's title and content Bla bla bla {{ client }} for {{ date }}. Bla bla bla.. Those parameters need to be set in notification's payload:
{ "name": "fcm-demo", "to": { "subscriberId": "sera-12345" }, "payload": { "client": "Serafeim", "date": "Thu, 4 May 2023 08:00" }, "overrides": { "fcm": { "data": { "action": "my_action", "url":"a url" } } } }
Kristof
KristofOP•3y ago
I know of the place holders, but I was wondering about logic in the place holders?
Pawan Jain
Pawan Jain•3y ago
Logic should also work
serafeimo
serafeimo•3y ago
Oh ok, we've implemented logic in the placeholders in the past and seemed fine.
Kristof
KristofOP•3y ago
I get this error
Kristof
KristofOP•3y ago
I also tried
Have a quick look at the company pages of {{companyNames.length === 2 ? `${companyNames[0]} and ${companyNames[1]}` : `${companyNames[0]}, ${companyNames[1]} ${companyNames[2]}`}} to check if they offer interesting opportunities or events for you!
Have a quick look at the company pages of {{companyNames.length === 2 ? `${companyNames[0]} and ${companyNames[1]}` : `${companyNames[0]}, ${companyNames[1]} ${companyNames[2]}`}} to check if they offer interesting opportunities or events for you!
but it gives the same error
Pawan Jain
Pawan Jain•3y ago
It can be the case that logic don't work. Can you please create feature request for this? !feature
Kristof
KristofOP•3y ago
GitHub
[NV-1749] 🚀 Feature: Support more logic in the templating. · Issue ...
🔖 Feature description It would be nice to have some more templating options available in titles and body fields. An example would be to pass a list of companyNames and be able to do this: {{company...
Gali Baum
Gali Baum•3y ago
Hi @kristofdt You could try this:
{{#each array}}
{{#if @last}} and
{{else}} {{#unless @first}} , {{/unless}}
{{/if}} {{this}}
{{/each}}
{{#each array}}
{{#if @last}} and
{{else}} {{#unless @first}} , {{/unless}}
{{/if}} {{this}}
{{/each}}
for
array: ["comp1","comp2","comp3"]
array: ["comp1","comp2","comp3"]
Result: comp1, comp2 and comp3
Kristof
KristofOP•3y ago
I was wondering if handlebars was supported. If this is the case then my feature request can be closed as well! thank you!
Gali Baum
Gali Baum•3y ago
sure 🙂
Kristof
KristofOP•3y ago
Might be interesting to add this to the documentation somewhere though?
Gali Baum
Gali Baum•3y ago
Templates | Novu
A template holds the entire flow of messages sent to the subscriber. This is where all the different channels are tied together under a single entity.
Gali Baum
Gali Baum•3y ago
But I agree, we should definitely expand and give more examples Feel free to ask for/add any missing helpers you find needed
Gali Baum
Gali Baum•3y ago
GitHub
novu/compile-template.usecase.ts at next · novuhq/novu
The open-source notification infrastructure with fully functional embedded notification center - novu/compile-template.usecase.ts at next · novuhq/novu
Gali Baum
Gali Baum•3y ago
If you still want to do the length comparison you can use {{#equals array.length 2}} .. {{else}}..{{/equals}}

Did you find this page helpful?