当前位置:早雪网网络学院编程文档其他语言 → 给PHPLIB模版加两个函数,解决模版文件内图片、CSS和JS包括路径问题

给PHPLIB模版加两个函数,解决模版文件内图片、CSS和JS包括路径问题

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:58:31
me) {

    if (!
is_array($varname)) {

      return
$this->varvals[$varname];

    } else {

      
reset($varname);

      while(list(
$k, $v) = each($varname)) {

        
$result[$k] = $this->varvals[$k];

      }

      

      return
$result;

    }

  }

  

  
/* public: get_undefined($handle)

   * handle: handle of a template.

   */

  
function get_undefined($handle) {

    if (!
$this->loadfile($handle)) {

      
$this->halt("get_undefined: unable to load $handle.");

      return
false;

    }

    

    
preg_match_all("/{([^}]+)}/", $this->get_var($handle), $m);

    
$m = $m[1];

    if (!
is_array($m))

      return
false;



    
reset($m);

    while(list(
$k, $v) = each($m)) {

      if (!isset(
$this->varkeys[$v]))

        
$result[$v] = $v;

    }

    

    if (
count($result))

      return
$result;

    else

      return
false;

  }



  
/* public: finish(string $str)

   * str: string to finish.

   */

  
function finish($str) {

    switch (
$this->unknowns) {

      case
"keep":

      break;

      

      case
"remove":

        
$str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);

      break;



      case
"comment":

        
$str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \1 undefined -->", $str);

      break;

    }

    

    return
$str;

  }



  
/* public: p(string $varname)

   * varname: name of variable to print.

   */

  
function p($varname) {

    print
$this->finish($this->get_var($varname));

  }



  function
get($varname) {

    return
$this->finish($this->get_var($varname));

  }

    

  
/***************************************************************************/

  /* private: filename($filename)

   * filename: name to be completed.

   */

  
function filename($filename) {

    if (
substr($filename, 0, 1) != "/") {

      
$filename = $this->root."/".$filename;

    }

    

    if (!
file_exists($filename))

      
$this->halt("filename: file $filename does not exist.");



    return
$filename;

  }

  

  
/* private: varname($varname)

   * varname: name of a replacement variable to be protected.

   */

  
function varname($varname) {

    return
preg_quote("{".$varname."}");

  }



  
/* private: loadfile(string $handle)

   * handle:  load file defined by handle, if it is not loaded yet.

   */

  
function loadfile($handle) {

    if (isset(
$this->varkeys[$handle]) and !empty($this->varvals[$handle]))

      return
true;



    if (!isset(
$this->file[$handle])) {

      
$this->halt("loadfile: $handle is not a valid handle.");

      return
false;

    }

    
$filename = $this->file[$handle];



    
$str = implode("", @file($filename));

    if (empty(
$str)) {

      
$this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");

      return
false;

    }

    

    
$str = $this->parsepath($str);//替换图片路径



    
$this->set_var($handle, $str);

    

    return
true;

  }

  

  
/*  重新分析替换模版目录下指定路径(一般为图片文件路径)

   */

  
function parsepath($str = ""){

      if(
"/" != substr($this->path, -1)){

        
$this->path .= "/";

    }

    if(
"./" == $this->path) return $str;

    
$str = preg_replace("|".$this->path."|", $this->root."/".$this->path, $str);

    return
$str;

  }



  
/***************************************************************************/

  /* public: halt(string $msg)

   * msg:    error message to show.

   */

  
function halt($msg) {

    
$this->last_error = $msg;

    

    if (
$this->halt_on_error != "no")

      
$this->haltmsg($msg);

    

    if (
$this->halt_on_error == "yes")

      die(
"<b>Halted.</b>");

    

    return
false;

  }

  

  
/* public, override: haltmsg($msg)

   * msg: error message to show.

   */

  
function haltmsg($msg) {

    
printf("<b>Template Error:</b> %s<br>\n", $msg);

  }

}

?>

<?

//这个声明表明moban目录和img目录在同一级目录下

$t = new Template("moban", "../img");



//这个表明img目录在moban目录里

$t = new Template("moban", "img");



?>

上一页  [1] [2] 

[数据载入中...] [返回上一页] [打 印]