Skip to content

result of template({}) is cached #1984

@bagheriali2001

Description

@bagheriali2001

Hi
We are using handlebars to fill out some dynamic messages. Today we noticed a very wierd bug in which result of template({}) is cached. No matter how many ways i tried, (I even imported handlebars in the function but it didn't fix the problem), I get the same result for each combination of variables.
As you can see in the code below, we even compile each template at time of use.

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:

❯ node handlebars.js
details:  { status: 'in_progress', ticket_num: 2013 }
text_template(variables):  THIS IS A TEST TICKET NUM: 2013 STATUS: in_progress
details:  { status: 'not_checked', ticket_num: 2008 }
text_template(variables):  THIS IS A TEST TICKET NUM: 2008 STATUS: not_checked
details:  { status: 'closed', ticket_num: 2017 }
text_template(variables):  THIS IS A TEST TICKET NUM: 2013 STATUS: in_progress
details:  { status: 'closed', ticket_num: 2009 }
text_template(variables):  THIS IS A TEST TICKET NUM: 2008 STATUS: not_checked

Environment:

  • Node version: 16.14.2 and 18.14.2
  • Handlebars package version: 4.7.8 and 4.7.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions