简单Jscript(ASP)模版操作文件

<%
/************************************************************************/
/* 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;
}
 %>

posted @ 2010-09-27 13:08  jifsu  阅读(294)  评论(0编辑  收藏  举报