handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.96k stars 2.04k forks source link

result of template({}) is cached #1984

Closed bagheriali2001 closed 1 year ago

bagheriali2001 commented 1 year ago

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: