<%
/************************************************************************/
/* JScript模版系統
@author jxfsuda
@date 2010-9-27
@see 此模版適應沒有FSO許可權的伺服器,且不準備把模版存放入資料庫的網站使用
模版預設路徑在檔案同級的template目錄下.
使用伺服器端的http請求擷取模版內容.
需要滿足條件: 伺服器上能訪問外網,且伺服器能訪問網站自身內容.
模版中變數書寫方式 ${變數名} , ${方法名(方法變數)} ,符合jstl寫法習慣
變數名 或者方法名,必須是已經存在的,且能訪問的
例: ${user.name} ${getArticle(0,1,false)}
沒事做個備份,用asp的小網站還是很多的
*/
/************************************************************************/
function template_split()
{
var len = 0;
var arr;
if(this[this.tplName]==null) return;
var template_exp =this.regex;
while(this[this.tplName].search(template_exp)!=-1)
{
arr = this[this.tplName].match(template_exp);
for(var i=0;i<arr.length;i++){
var key = arr[i].slice(2,-1);
try{
this[this.tplName] = this[this.tplName].replace(arr[i],eval(key));
}catch(ex){
this[this.tplName] = this[this.tplName].replace(arr[i],"錯誤: "+ex.message);
}
}
}
}
function template_load(filename)
{
try{
var http = Server.CreateObject("MSXML2.ServerXMLHTTP");
http.setOption ( 2 , 13056 );
var thisHost = Request.ServerVariables("HTTP_HOST");
var thisDir = Request.ServerVariables("PATH_INFO").Item;
var pathInfo = thisDir.substring(0,thisDir.lastIndexOf("/")+1);
var url = "http://"+ thisHost + pathInfo + "template/"+ filename;
http.open("GET",url, false );
http.send();
var resp = http.responseText.replace("\r\n","");
// Response.Write(resp);
this[this.tplName] =resp;
this.split();
}catch(ex){
this[this.tplName] ="錯誤:模版讀取錯誤,"+ex.message;
}
}
//最終顯示
function template_show(){
Response.Write(this.content());
}
function template_Content(){
return this[this.tplName];
}
/************************************************************************/
/*
@see
使用方法
var tpl = new template(); //初始化
tpl.load("index.htm"); //載入模版
tpl.show(); //顯示模版
tpl=null; //釋放資源 */
/************************************************************************/
function template(path)
{
if(path==null || path===undefined) path="template";
this.tplpath = path;
this.regex = new RegExp("\\$ *\\{ *(\\w* *(\\( *([^)]*? *) *\\)){0,}) *\\}","ig");
this.tplName="tplContent"; //變數名和值不能一樣,否則會被覆蓋
//method
this.split = template_split;
this.load = template_load;
this.show = template_show;
this.content = template_Content;
}
%>