/** * Template module for simple javascript templating */ (function(){ "use strict"; //This module relies on some others for simplicity //so, if they aren't there, don't initialize the module if($_.ajax === "undefined") { return; } var t, _t, _p; //Private object to store retrieved templates _t = {}; //Private object to store parsed templates _p = {}; /** * Module for html templating. Requires ajax module. * * @name template * @namespace * @memberOf $_ */ t = { /** * Retrieves a template * * @memberOf $_.template * @name get * @param string name * @return string * @function * @type string */ get: function(name) { var res; res = this.el.innerHTML; if(res === "") { console.log("Template is empty or cannot be found"); return; } _t[name] = res; return res; }, /** * Formats a template * * @memberOf $_.template * @name parse * @param string template_name * @param object replace_data * @return string * @function * @type string */ parse: function(name, data) { var tmp = _t[name], pairs = [], pair_reg = /\{([A-Z0-9_\-]+)\}(.*)\{\/\1\}/gim, var_reg = /\{([A-Z0-9_\-]+)\}/gim, pseudos = [], num_pairs = 0, num_pseudos = 0, i = 0, j = 0, var_name = '', rep_data = {}, tmp_data = '', data_len, frag, frag_section, emptys, x; tmp = String(tmp); //Remove newlines and tabs from template because //those whitespace characters are extra bandwidth tmp = tmp.replace(/\s+/gim, " "); tmp = tmp.replace(/>\s+<"); tmp = tmp.replace(/>\s+\{/gim, ">{"); tmp = tmp.replace(/\}\s+ 0) { data_len = rep_data.length; //Get rid of the loop tags pairs[i] = pairs[i].replace(pair_reg, "$2"); //Replace psudovariables with data for(j=0;j