最近自己在写一个系统,懒得学习sm 和pl而且我觉得也没必要扛那么大的一门炮,所以借鉴DZ写了一个模版类。
功能:条件判断、foreach 循环、for循环、计算好的函数直接放到模版无需在编译(我比较懒,这个也是我写这个模版类的原因)、直接在模版中调用任何php内置函数或自定义函数。模版采用缓存
复制代码
使用示例:
以后台./admin/index.php的调用为例,
./admin/index.php
复制代码
以下是模版./templates/index.html
功能:条件判断、foreach 循环、for循环、计算好的函数直接放到模版无需在编译(我比较懒,这个也是我写这个模版类的原因)、直接在模版中调用任何php内置函数或自定义函数。模版采用缓存
- <?php
- /* 村民:52php */
- /* mail: insuns@gmail.com */
- /* 2007- 4 -5 */
- class Template{
- var $tpl_data = '';
- var $root = ''; //模版对网站根地址的路径,比如‘.'或者'./../’;
- var $tpl_dir = 'templates';//模版文件夹
- var $cache_dir = 'cache'; //模版缓存文件夹
- var $flush = 12; ///12个小时更新一次
- var $files = '';
- var $ext = '.html'; //模版文件的后缀名
- function Template($root)
- {
- $this->root = $root;
- }
- function parse($files)
- {
- $this->files = $this->root . $this->tpl_dir . '/' . $files . $this->ext;
- $this->cache = $this->root . $this->cache_dir . '/' . $files.'.php';
- if(@file_exists($this->cache)) //cache文件存在
- {
- $fmtime = @filemtime($this->cache); //检查cache的修改时间
- if(($fmtime + $this->flush * 3600) < time()) //cache过时则重建并返回
- {
- return $this->write_to_cache($this->files);
- }
- else return $this->cache;
- }
- else //cache不存在则直接编译模版
- {
- return $this->write_to_cache($this->files);
- }
- }
- function update($files)
- {
- $this->flush = 0;
- if(is_array($files))
- {
- list($key,$handle) = each($files);
- $this->parse($handle);
- }
- else $this->parse($files);
- }
- function write_to_cache($files)
- {
- $this->tpl_data = $this->compiled($files);
- $state = @file_put_contents($this->cache,$this->tpl_data,LOCK_EX);
- if (!$state) die('写入模版 '.$this->files.' 的缓存文件时失败,请检查相应目录及文件的属性!<br>'.$this->cache);
- else return $this->cache;
- }
- function compiled($files)
- {
- $data .= '<?php if(!defined(\'IN_INSUN\'))'."\t".'die("Hacking attempt!");?>'."\n";
- if (!$temp = @file($files)) die('Failed to open template file '.$files);
- $data .= implode('',$temp);
- $data = preg_replace('/\{(\$.+?)\}/',"<?=\\1;?>",$data);
- $data = preg_replace('/\{LOOP\_FOR\s([\$a-zA-Z]+)\=([\d]+)\scount\((.+)\)\}/','<? for (\\1=\\2;\\1<count(\\3);\\1++){ ?>',$data);
- $data = preg_replace('/\{LOOP\s(\$.+?)\s(\$.+?)\}/','<? foreach(\\1 as \\2) {?>',$data);
- $data = str_replace(array('{/LOOP_FOR}','{/LOOP}'),'<? }?>',$data);
- $data = preg_replace('/\{FUN\s(.+?)\}/',"<?=\\1;?>",$data);
- $data = preg_replace('/\{(ELSE)?IF\s(.+?)}/','<? \\1IF(\\2) {?>',$data);
- $data = str_replace(array('{ELSE}','{/IF}'),array('<? } ELSE { ?>','<? }?>'),$data);
- return $data;
- }
- }
- ?>
以后台./admin/index.php的调用为例,
./admin/index.php
- <?php
- define('IN_INSUN',1);
- ///以下的$test函数为测试函数,当然你可以把你计算完的任何函数放到模版中不用进行其他的换算
- $test = 'This just a test template';
- $url = 'http://163.com';
- /////以下调用模版类进行编译
- $admin_root_path = './../'; //定义后台对跟目录的路径
- $admin_tpl_dir = 'admin/'; ///后台模板缓存文件夹
- require_once './../my_class/template.php';
- $tp = new Template($admin_root_path);
- include $tp->parse($admin_tpl_dir.'test'); //引入后台首页模板
- ?>
- <html>
- <head>
- <title>test template</title>
- </head>
- <body>
- <div style="position:absolute;top:30%;left:10%;right:10%;font-size:28px;font-weight:bold">{$test}<br/>这个是调用htmlspecialchars 函数示例,你可以调用任何php函数或者自定义函数。但为了区别你通过赋值或计算的变量,需在{后面加上大写的FUN如:{FUN htmlspecialchars($url)}</div>
- </body>
- </html>
复制代码
调用说明
变量:在模版中用 {变量}
函数:在模版中用{FUN 函数}
条件判断:{IF 条件} 。。。{ELSEIF 条件} 。。。{ELSE} 。。。{/IF}
for循环:{LOOP_FOR 初始值 最大值}循环体{/LOOP}
foreach循环:{LOOP 原始变量 foreach后的变量} 循环体{/LOOP}
以下是我一个模版实体部分的调用,除了for循环外其他的调用都有,大家看下,有不清楚的留言:
此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/29866.html
变量:在模版中用 {变量}
函数:在模版中用{FUN 函数}
条件判断:{IF 条件} 。。。{ELSEIF 条件} 。。。{ELSE} 。。。{/IF}
for循环:{LOOP_FOR 初始值 最大值}循环体{/LOOP}
foreach循环:{LOOP 原始变量 foreach后的变量} 循环体{/LOOP}
以下是我一个模版实体部分的调用,除了for循环外其他的调用都有,大家看下,有不清楚的留言:
- <div id="wrap">
- <form action="" method="post" name="form1">
- <table border="0" width="96%" align="center">
- <tr class="{FUN getbg()}">
- <td colspan="10" align="right">
- <table width="100%" align="center" border="0">
- <tr>
- <td>
- <a href="index.php?action={$archive_table[$channel_id].'_manage'}&do=add&channel_id={$channel_id}">添加文档</a>
- </td>
- <td>
- <a href="index.php?action={$archive_table[$channel_id].'_manage'}&do=validate&channel_id={$channel_id}">审核</a>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- {IF $channel_id == '3'}
- <tr class="{FUN getbg()}" align="center">
- <td width="5%"><strong>ID</strong></td>
- <td width="5%"><strong>选择</strong></td>
- <td width="20%"><strong>歌名</strong></td>
- <td width="9%"><strong>分类</strong></td>
- <td width="9%"><strong>专辑</strong></td>
- <td width="9%"><strong>歌手</strong></td>
- <td width="5%"><strong>点击数</strong></td>
- <td width="10%"><strong>创建时间</strong></td>
- <td width="10%"><strong>修改时间</strong></td>
- <td width="18%"><strong>操作</strong></td>
- </tr>
- {LOOP $rows $arc}
- <tr class="{FUN getbg()}" align="center">
- <td>{$arc['music_id']}</td>
- <td><input type="checkbox" name="arcid" id="arcid" value="{$arc['music_id']}"/></td>
- <td><a href="index.php?action=music_manage&do=edit&music_id={$arc['music_id']}">{$arc['title']}</a></td>
- <td>{$arc['name']}</td>
- <td>{$arc['album_name']}</td>
- <td>{$arc['singer_name']}</td>
- <td>{$arc['click']}</td>
- <td>{$arc['post_time']}</td>
- <td>{$arc['last_update']}</td>
- <td><a href="{$insun_root_path.$channel_id.'/'.$cat_id.'/'.$arc['music_id'].EXT}">浏览文档</a> | <a href="index.php?action=music_manage&do=edit&music_id={$arc['music_id']}">编辑</a> | <a href="index.php?action=music_manage&do=del&music_id={$arc['music_id']}&name={FUN urlencode($arc['title'])}">删除</a></td>
- </tr>
- {/LOOP}
- {/IF}
- {IF $totalPage>=1}
- <tr class="{FUN getbg()}">
- <td colspan="10" align="center">{$page_nav}</td>
- </tr>
- {/IF}
- {IF $rows[0]['name']}
- <tr class="{FUN getbg()}">
- <td colspan="10">
- <input type="radio" name="action" value="recommend" />推荐
- <input type="radio" name="action" value="move" />移动
- <input type="radio" name="action" value="del" />删除
- <input type="button" onclick="CheckAll(this.form)" class="submit little" value="全选">
- <input type="button" name="submit" onclick="CheckOthers(this.form)" class="submit little" value="反选">
- <input type="submit" value="Submit" class="submit little" />
- </td>
- </tr>
- {ELSE}
- <tr class="{FUN getbg()}">
- <td colspan="10" align="center">目前尚无文档!</td>
- </tr>
- {/IF}
- </table>
- </form>
- </div>
复制代码
for循环调用示例:
复制代码
模版缓存更新请调用update 如:$tp->update($admin_tpl_dir.'test');
update中的文件可以是array,需要注意的是路径问题,如果后台模版是在./templates/admin/,那么后台调用模版文件就要用admn/file.html
这个用的比较少,所以各位如果需要用到请自己测试。
这个模版并不适合美工用,因为我是自己写程序和模版,所以只为方便写的,没考虑通用性,我考虑的是实用性和方便。当然,因为我的系统并没写完,所以功能上增减也是必然。如果发现bug请报告给我,谢谢
- {LOOP_FOR $i=0 count($test)}
- <li>$test</li>
- {/LOOP}
update中的文件可以是array,需要注意的是路径问题,如果后台模版是在./templates/admin/,那么后台调用模版文件就要用admn/file.html
这个用的比较少,所以各位如果需要用到请自己测试。
这个模版并不适合美工用,因为我是自己写程序和模版,所以只为方便写的,没考虑通用性,我考虑的是实用性和方便。当然,因为我的系统并没写完,所以功能上增减也是必然。如果发现bug请报告给我,谢谢
不好意思,事例中的:$admin_tpl_dir = 'admin/'; ///后台模板缓存文件夹 错了。这个应该是$admin_tpl_dir = 'admin/'; ///后台模板文件夹 也就是后台模版对模版文件夹的地址,如果你是所有的模版都放在一起的就不用定义
- <?php
- define('IN_INSUN',1);
- ///以下的$test函数为测试函数,当然你可以把你计算完的任何函数放到模版中不用进行其他的换算
- $test = 'This just a test template';
- $url = 'http://163.com';
- /////以下调用模版类进行编译
- $admin_root_path = './../'; //定义后台对跟目录的路径
- $admin_tpl_dir = 'admin/'; ///后台模板文件夹
- require_once './../my_class/template.php';
- $tp = new Template($admin_root_path);
- include $tp->parse($admin_tpl_dir.'test'); //引入后台首页模板
- ?>