I have an HTML file containing various templates that I want to compile when the page loads. I'm looking for a way to either compile and store all templates in an array upfront, or compile templates on the fly as users navigate through the SPA. I am using JS and Handlebars.js for this project. Can you offer any guidance on how to accomplish this? Currently, I have a function that needs modification and implementation:
function compile_template(template) {
var template_source;
if (templates_compiled[template] === undefined)
{
template_source = $("#" + template).html();
return Handlebars.compile(template_source);
}
}
To implement the function, I use the following code:
template = "template_projects";
templates_compiled[template] = compile_template(template);
$("#div_page_data_container").html(templates_compiled[template](page_data));