📄 zptemplate-core.js
字号:
tag: '', args: '' };};/** * Parses expression tag and returns result. Must be called in scope of Template * object. * * @private * @param {object} aOutTpl Output array with tokens * @param {object} aOutTags Output array with functions replacing tags * @param {object} aTokens Input array with tokens * @param {number} iToken Current position in the input array * @return New position in the input array (here always the same as iToken) * @type number */Zapatec.Template.parseExprOut = function(aOutTpl, aOutTags, aTokens, iToken) { // Form tag function aOutTags.push(new Function('zpTemplateIndex', [ 'var zpTemplateVars=this.vars;return ', aTokens[iToken].args ].join(''))); // Place for the function return value aOutTpl.push(''); return iToken;};/** * Parses expression tag and doesn't return result. Must be called in scope of * Template object. * * @private * @param {object} aOutTpl Output array with tokens * @param {object} aOutTags Output array with functions replacing tags * @param {object} aTokens Input array with tokens * @param {number} iToken Current position in the input array * @return New position in the input array (here always the same as iToken) * @type number */Zapatec.Template.parseExpr = function(aOutTpl, aOutTags, aTokens, iToken) { // Form tag function aOutTags.push(new Function('zpTemplateIndex', [ 'var zpTemplateVars=this.vars;', aTokens[iToken].args, ';return ""' ].join(''))); // Place for the function return value aOutTpl.push(''); return iToken;};/** * Parses condition tag. Must be called in scope of Template object. * * @private * @param {object} aOutTpl Output array with tokens * @param {object} aOutTags Output array with functions replacing tags * @param {object} aTokens Input array with tokens * @param {number} iToken Current position in the input array * @return New position in the input array * @type number */Zapatec.Template.parseCond = function(aOutTpl, aOutTags, aTokens, iToken) { var aTemplates = this.templates; var aTags = this.tags; var aNewTemplate = []; var aNewTag = []; aTemplates.push(aNewTemplate); aTags.push(aNewTag); var iTemplate = aTemplates.length - 1; var oSyntax = this.config.syntax; // Form tag function var aFunc = [ 'var zpTemplateVars=this.vars;if(', aTokens[iToken].args, '){return this.compileTemplate(', iTemplate.toString(), ',zpTemplateIndex)' ]; var iTokens = aTokens.length; var oToken, sTag, oRule; for (iToken++; iToken < iTokens; iToken++) { oToken = aTokens[iToken]; if (typeof oToken == 'object') { // Tag sTag = oToken.tag; if (sTag == 'else') { aNewTemplate = []; aNewTag = []; aTemplates.push(aNewTemplate); aTags.push(aNewTag); iTemplate = aTemplates.length - 1; aFunc.push('}else{return this.compileTemplate('); aFunc.push(iTemplate.toString()); aFunc.push(',zpTemplateIndex)'); } else if (sTag == 'elseif') { aNewTemplate = []; aNewTag = []; aTemplates.push(aNewTemplate); aTags.push(aNewTag); iTemplate = aTemplates.length - 1; aFunc.push('}else if('); aFunc.push(oToken.args); aFunc.push('){return this.compileTemplate('); aFunc.push(iTemplate.toString()); aFunc.push(',zpTemplateIndex)'); } else if (sTag == 'endif') { break; } else { oRule = oSyntax[sTag]; if (oRule) { iToken = oRule.parser.call(this, aNewTemplate, aNewTag, aTokens, iToken); } } } else { // String aNewTemplate.push(oToken); // No tag in this position aNewTag.push(null); } } aFunc.push('}'); aOutTags.push(new Function('zpTemplateIndex', aFunc.join(''))); // Place for the function return value aOutTpl.push(''); return iToken;};/** * Parses loop tag. Must be called in scope of Template object. * * @private * @param {object} aOutTpl Output array with tokens * @param {object} aOutTags Output array with functions replacing tags * @param {object} aTokens Input array with tokens * @param {number} iToken Current position in the input array * @return New position in the input array * @type number */Zapatec.Template.parseLoop = function(aOutTpl, aOutTags, aTokens, iToken) { var aTemplates = this.templates; var aTags = this.tags; var aNewTemplate = []; var aNewTag = []; aTemplates.push(aNewTemplate); aTags.push(aNewTag); var iTemplate = aTemplates.length - 1; var oSyntax = this.config.syntax; // Form tag function var aFunc = [ 'var zpTemplateVars=this.vars;var zpTemplateIndex=0;var zpTemplateArray=(', aTokens[iToken].args, ');if(!(zpTemplateArray instanceof Array)){return ""};var zpTemplateArrayLen=zpTemplateArray.length;var zpTemplateTpl=[];for(;zpTemplateIndex<zpTemplateArrayLen;zpTemplateIndex++){zpTemplateTpl.push(this.compileTemplate(', iTemplate.toString(), ',zpTemplateIndex))};return zpTemplateTpl.join("")' ]; var iTokens = aTokens.length; var oToken, sTag, oRule; for (iToken++; iToken < iTokens; iToken++) { oToken = aTokens[iToken]; if (typeof oToken == 'object') { // Tag sTag = oToken.tag; if (sTag == 'endfor') { break; } else { oRule = oSyntax[sTag]; if (oRule) { iToken = oRule.parser.call(this, aNewTemplate, aNewTag, aTokens, iToken); } } } else { // String aNewTemplate.push(oToken); // No tag in this position aNewTag.push(null); } } aOutTags.push(new Function(aFunc.join(''))); // Place for the function return value aOutTpl.push(''); return iToken;};/** * Parses comment tag and ignores everything inside. Must be called in scope of * Template object. * * @private * @param {object} aOutTpl Output array with tokens * @param {object} aOutTags Output array with functions replacing tags * @param {object} aTokens Input array with tokens * @param {number} iToken Current position in the input array * @return New position in the input array (here always the same as iToken) * @type number */Zapatec.Template.parseComment = function(aOutTpl, aOutTags, aTokens, iToken) { // Skip everything inside comment var iTokens = aTokens.length; var oToken; for (iToken++; iToken < iTokens; iToken++) { oToken = aTokens[iToken]; if (typeof oToken == 'object') { // Tag if (oToken.tag == 'endrem') { break; } } } return iToken;};/** * Assigns a value to the template variable. * * @param {string} sVar Template variable name * @param {string} sVal New variable value */Zapatec.Template.prototype.assign = function(sVar, sVal) { this.vars[sVar] = sVal;};/** * Assigns multiple template variables at once. * * @param {object} oArg Associative array with variable name - value pairs */Zapatec.Template.prototype.assignVars = function(oArg) { var oVars = this.vars; var sVar; for (sVar in oArg) { oVars[sVar] = oArg[sVar]; }};/** * Replaces all template variables. * * @param {object} oArg Associative array with variable name - value pairs */Zapatec.Template.prototype.reassignVars = function(oArg) { // Remove old values this.vars = {}; // Assign new values this.assignVars(oArg);};/** * Compiles template into a string. * * @private * @param {number} iTpl Index in the internal templates array * @param {number} iLoopIndex Current loop index * @return Compiled template * @type string */Zapatec.Template.prototype.compileTemplate = function(iTpl, iLoopIndex) { var aTpl = this.templates[iTpl]; var aTags = this.tags[iTpl]; var iTags = aTags.length; var iTag, fTag; try { for (iTag = 0; iTag < iTags; iTag++) { fTag = aTags[iTag]; if (typeof fTag == 'function') { // Call in scope of this object aTpl[iTag] = fTag.call(this, iLoopIndex); } } } catch (oExpn) { var oConf = this.config; alert(oExpn.message.replace(/zpTemplateVars\.(\w+)/g, [oConf.varLeftDelimiter, '$1', oConf.varRightDelimiter].join('')).replace( /zpTemplateIndex/g, oConf.loopIndex)); } return aTpl.join('');};/** * Compiles template into a string. Before compilation assigns variable values * passed in the argument. * * @param {object} oArg Optional. Associative array with variable name - value * pairs * @return Compiled template * @type string */Zapatec.Template.prototype.compile = function(oArg) { // Assign variables if (oArg) { this.assignVars(oArg); } // Compile template return this.compileTemplate(0);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -