| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /**
- * Template - Very Very Simple Template Engine
- * similar prototype.js template engine
- * add syntax > #{for:name:maxCount:cutCount} template #{/for:name}
- * add syntax > #{if:name sign value} template #{/if:name}
- */
- (function(){
-
- function evaluate(data, tpl) {
- if (!data) {
- return '';
- }
- if (tpl.indexOf("\{if:") > -1) {
- tpl = tpl.replace(/#\{if:([_\w]+)([=><!]+)([_'"\-\w]+)\}([\s\S]*?)#\{\/if:\1\}/gm, function(full, start, sign, value, condtpl){
- if (data[start] == _NULL) {
- return full;
- }
- var _condition = _FALSE;
- try {
- sign = ((sign=="=")? "==": sign);
- var _left = "\"" + (data[start] + "").replace(/['"]/g, "") + "\"";
- var _right = "\"" + value.replace(/['"]/g, "") + "\"";
- _condition = txEval("(" + _left + sign + _right + ")");
- }catch(e) { _condition = _FALSE; }
- if(_condition) {
- return evaluate(data, condtpl);
- } else {
- return "";
- }
- });
- }
- if (tpl.indexOf("\{for:") > -1) {
- tpl = tpl.replace(/#\{for:([_\w]+):?(\d*):?(\d*)\}([\s\S]*?)#\{\/for:\1\}/gm, function(full, start, maxCnt, cutCnt, looptpl) {
- if (!data[start] || !data[start].length) {
- return full;
- }
- var _list = data[start];
- var _listTpl = [];
- maxCnt = !!maxCnt? (isNaN(maxCnt)? _list.length: parseInt(maxCnt)): _list.length;
- cutCnt = !!cutCnt? (isNaN(cutCnt)? 0: parseInt(cutCnt)): 0;
- for (var i = 0, len = Math.min(_list.length, maxCnt); i < len; i++) {
- _listTpl.push(evaluate(_list[i], looptpl));
- }
- return _listTpl.join("").substring(cutCnt);
- });
- }
- return tpl.replace(/#\{([_\w]+)\}/g, function(full, name) {
- if(data[name] != _NULL) {
- return data[name];
- } else {
- return full;
- }
- });
- }
-
- var tmp = _WIN.Template = function(template) {
- this.template = template;
- };
-
- tmp.prototype = {
- evaluate : function(data) {
- return evaluate(data, this.template);
- },
- evaluateToDom : function(data, element) {
- if(typeof(element) === 'string') {
- element = _DOC.getElementById(element);
- }
- element.innerHTML = evaluate(data, this.template);
- },
- evaluateAsDom : function(data, context) {
- var _tmpNode = (context || document).createElement('div');
- _tmpNode.innerHTML = evaluate(data, this.template);
- return _tmpNode.firstChild;
- }
- };
- /*
- Template.prototype.evaluate = function(data) {
- return evaluate(data, this.template);
- };
-
- Template.prototype.evaluateToDom = function(data, element) {
- if(typeof(element) === 'string') {
- element = _DOC.getElementById(element);
- }
- element.innerHTML = evaluate(data, this.template);
- };
-
- Template.prototype.evaluateAsDom = function(data, context) {
- var _tmpNode = (context || document).createElement('div');
- _tmpNode.innerHTML = evaluate(data, this.template);
- return _tmpNode.firstChild;
- };
- */
- })();
|