簡單Jscript(ASP)模版操作檔案

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.