⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swato-jst.js

📁 JS库
💻 JS
字号:
/*
 *  (c) 2005 Zhijie Chen <zigzag.chen@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var JSTemplate = {
	process: function(template,context) {
		var parseResult;
		var functionText=["var tempFunction=function(context,stack) { with (context) {"];
		functionText.push("out=[];");
		var lines = template.split('\n');
		for (var i = 0; i < lines.length; i++) {
			this._parseLine(lines[i], functionText);
		}
		functionText.push("return out.join(\"\\n\");");
		functionText.push("}}");
		try {
			eval(functionText.join(""));
		} catch (e) {
			alert(functionText.join("\n"));
			alert("eval ERROR("+ e.toString()+"). Check for unclosed, unmatched statement(s).");
			return;
		}

		var stack=[];
		try {
			parseResult = tempFunction(context,stack);
		} catch (e) {
			alert("ERROR("+e.toString()+") at \""+stack[stack.length-1]+"\"");
		}
		return parseResult;
	},

	_parseLine:function(line,functionText) {
		var escaped=line.replace("\r","");
		escaped=escaped.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
		escaped=escaped.replace(/<!--begin/g,"<!--begin-->").replace(/end-->/g,"<!--end-->");
		var prefix;
		if (line.charAt(0)=='#') {
			prefix="stack.push(\""+escaped.replace(/\"/g,"\\\"")+ "\");\n";
			functionText.push(prefix+escaped.substring(1,escaped.length));
		} else {
			escaped=escaped.replace(/\"/g,"\\\"").replace(/<%/g,"\"+").replace(/%>/g,"+\"");
			prefix="stack.push(\""+escaped+ "\");\n";
			functionText.push(prefix+"out.push(\""+escaped+"\");");
		}
	}
};

Swato.TemplateDiv = Class.create();
Swato.TemplateDiv.prototype = {
	initialize: function(dispDivName,templateName) {
		this.dispDivName = dispDivName;
		if (document.getElementById(templateName)!=null) {
			this.template = $(templateName).innerHTML;
		} else {
			var option= new Object();
			option.onComplete=this._gotTemplate.bind(this);
			new Ajax.Request(templateName,option);
		}
	},

	_gotTemplate : function(response){
		this.template=response.responseText;
	},
	
	process: function(context) {
		if  (this.dispDivName==null) {
			return JSTemplate.process(this.template, context);
		} else {
			$(this.dispDivName).innerHTML= JSTemplate.process(this.template, context);
		}
	},

	gotResult: function(result) {
//		$(this.dispDivName).innerHTML= JSTemplate.process(this.template, result);
		this.process(result);
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -