Blog

Easiest JQuery Template

Here is the simplest JQuery template possible. It will get the template code from a div and replace the variables.

 

function template(templateId, data) {
        var data = typeof data === "undefined" ? {} : data;
        var tpl = $('#'+templateId).html();
        $.each(data, function (key, value) {
            tpl = tpl.split('$$' + key + '$$').join(value);
        });
        tpl = tpl.replace(new RegExp("\\$\\$.*\\$\\$", "g"), "");
        return tpl;
}