- 论坛徽章:
- 0
|
啥都不说了,看代码吧!
- <?php
- /*
- *名称: assets模版
- *版本: 1.16
- *作者: achun
- *email: achun.shx@gmail.com
- *版权: BSD
- *简单介绍:
- * 根据axgle的assettemplate 改造的更精简的模版
- * 其实就是自动加上烦人的echo<<<EOT 和 EOT;
- * 吸取了assettemplate中$asset的技巧
- * 因为更简单所以命名为assets
- *
- */
- class assets {
- /*
- *模版所在目录
- *加上..的目的很简单,../tpl在网站/目录的上层,这样用户就不能访问了.
- */
- var $tpl_dir = '../tpl';
- /*
- *道理和$tpl_dir一样,不过由于在我的设计里只有一个入口就是index.php
- *所有可以这样安排目录,如果你的需求不是这样就要改改了
- */
- var $cpl_dir= '../tpl_c';
- /*
- *asset的路径了,这个的作用axgle说的很好
- *不明白的还是找找axgle的文章吧.
- */
- var $asset_dir;
- function assets($dir='asset') {
- $this->asset_dir=$dir;
- }
- /*
- *整个模版的编译就在这里,就是字符串替换了
- *例子:
- <!---begin--->
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script type="text/javascript" src="$asset/js/prototype.js" />
- <link rel="stylesheet" rev="stylesheet" href="/$asset/css/style.css" type="text/css" media="all" />
- <!---if($_SERVER['REMOTE_ADDR']=='127.0.0.1'){--->
- <title>本地访问</title>
- <!---}else{--->
- <title>远程访问</title>
- <!---}--->
- </head>
- <body>
- </body>
- </html>
- <!---end--->
- *说明:
- * 如果模版中有php的代码就必须用<!---begin--->和<!---end--->
- * 把它们包起来,所有的php代码都用<!---code--->的形式写
- * 注意{}的用法,switch也可以支持自己试试就知道具体的写法了.
- */
- function _compiled($tpl,&$cpl_file){
- $tpl_file=$this->tpl_dir."/$tpl";
- $cpl_file=$this->cpl_dir."/$tpl.htm";
- if(!file_exists($tpl_file)) return false;
- if(file_exists($cpl_file)){
- if(filemtime($cpl_file) >= filemtime($tpl_file)) return true;
- }
- $data=$this->_read($tpl_file);
- $data=str_replace('<!---begin--->','<?php'."\r\n".'echo<<<EOT',$data);
- $data=str_replace('<!---end--->',"\r\n".'EOT;'."\r\n".'?>',$data);
- $data=str_replace('<!---',"\r\n".'EOT;'."\r\n",$data);
- $data=str_replace('--->',"\r\n".'echo<<<EOT',$data);
- return $this->_write($cpl_file,$data);
- }
- function display($tpl){
- $cpl_file='';
- if(!$this->_compiled($tpl,$cpl_file)){
- return false;
- }
- extract((array)$this);
- $asset='$'.$this->asset_dir;
- include $cpl_file;
- return true;
- }
- function fetch($tpl) {
- ob_start();
- $this->display($tpl);
- $data=ob_get_contents();
- ob_end_clean();
- return $data;
- }
- function _read($filename,$mode="rb") {
- if($fp=@fopen($filename,$mode)) {
- flock($fp,LOCK_SH);
- $read_data=fread($fp,filesize($filename));
- fclose($fp);
- }
- return $read_data;
- }
- function _write($filename,$data,$mode="wb") {
- if($fp=@fopen($filename,$mode)) {
- flock($fp,LOCK_EX);
- fwrite($fp,$data);
- fclose($fp);
- return true;
- }
- return false;
- }
- }
- ?>
复制代码 |
|