result of template({}) is cached #1985
-
Hi Example Code to reproduce the bug: const handlebars = require("handlebars");
const notification_messages = {
values: [
{
id: "NOTIFICATION_ID",
type: "support",
text: "THIS IS A TEST TICKET NUM: {{ticket_num}} STATUS: {{status}}",
},
{
id: "NOTIFICATION_ID2",
type: "support",
text: "THIS IS A TEST TICKET NUM: {{ticket_num}} STATUS: {{status}}",
},
],
get_message: function(id) {
for (let index = 0; index < this.values.length; index++) {
let item = this.values[index];
if(item.id === id) {
return item;
}
}
},
};
const notification_message_populater = async (notification_id, details) => {
let notification_data = notification_messages.get_message(notification_id);
console.log("details: ", details)
const text_template = handlebars.compile(notification_data.text);
console.log("text_template(variables): ", text_template(details))
notification_data.text = text_template(details)
return notification_data;
}
notification_message_populater("NOTIFICATION_ID",{ status: 'in_progress', ticket_num: 2013 })
notification_message_populater("NOTIFICATION_ID2",{ status: 'not_checked', ticket_num: 2008 })
notification_message_populater("NOTIFICATION_ID",{ status: 'closed', ticket_num: 2017 })
notification_message_populater("NOTIFICATION_ID2",{ status: 'closed', ticket_num: 2009 }) And the result is:
Environment:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
By assigning the rendered template to your template-object ( This will work as expected: const handlebars = require("handlebars");
const notification_messages = {
values: [
{
id: "NOTIFICATION_ID",
type: "support",
text: "THIS IS A TEST TICKET NUM: {{ticket_num}} STATUS: {{status}}",
},
{
id: "NOTIFICATION_ID2",
type: "support",
text: "THIS IS A TEST TICKET NUM: {{ticket_num}} STATUS: {{status}}",
},
],
get_message: function(id) {
for (let index = 0; index < this.values.length; index++) {
let item = this.values[index];
if(item.id === id) {
return item;
}
}
},
};
const notification_message_populater = async (notification_id, details) => {
let notification_data = notification_messages.get_message(notification_id);
console.log("details: ", details)
const text_template = handlebars.compile(notification_data.text);
console.log("text_template(variables): ", text_template(details))
// Don't overwrite your own template
// notification_data.text = text_template(details)
return notification_data;
}
notification_message_populater("NOTIFICATION_ID",{ status: 'in_progress', ticket_num: 2013 })
notification_message_populater("NOTIFICATION_ID2",{ status: 'not_checked', ticket_num: 2008 })
notification_message_populater("NOTIFICATION_ID",{ status: 'closed', ticket_num: 2017 })
notification_message_populater("NOTIFICATION_ID2",{ status: 'closed', ticket_num: 2009 }) |
Beta Was this translation helpful? Give feedback.
By assigning the rendered template to your template-object (
notification_data.text = text_template(details)
), you are overwriting yournotification_messages
-templates with the rendered template, thus the template gets replaced with the result from the firsttext_template(...)
-call.This will work as expected: