<?php
class templateClass{
var $template_file ;//模板存放
var $template_cache_file ;//编译过后的模板
var $template_data ;//模板数据获取方式(文件/数据库)
var $t_hx = '.html';//模板后续
var $tc_hx = '.tpl.php';//编译过后模板的后续
/**
* 析构函数
*
* @param string $template_file //模板存放路径
* @param string $template_cache_file //编译过后的模板存放路径
*/
function __construct($template_file= "template",$template_cache_file= "template/template_cache",$template_data='file'){
$this->template_file = $template_file;
$this->template_cache_file = $template_cache_file;
$this->template_data = $template_data;
}
/**
* 输出错误信息
*
* @param string $msg
*/
function error($msg){
print "<div style=\"font-size:12px;color:red;\">".$msg."</div>";
}
/**
* 编译模板(创建,更新用)
*
* @param string $module
* @param string $template
* @return string 失败返回0 成功返回编译过后的地址
*/
function template_compile($module,$template)
{
try{
if($this->template_data='file'){
$content = file_get_contents($this->template_file.'/'.$module.'/'.$template.$this->t_hx);
}else{
//数据库方式获取
}
$content = $this->template_parse($content);
$compiledtplfile = $this->template_cache_file.'/'.$module.'_'.$template.$this->tc_hx;
$strlen = file_put_contents($compiledtplfile, $content);
@chmod($compiledtplfile, 0777);
return $compiledtplfile;
}catch (Exception $e){
return 0;
}
}
/**
* 获取Tpl编译过后的模板缓存文件
*
* @param string $module
* @param string $template
*/
function template_getTpl($module,$template)
{
$templateFile = $this->template_file.'/'.$module.'/'.$template.$this->t_hx;
$compiledtplfile = $this->template_cache_file.'/'.$module.'_'.$template.$this->tc_hx;
//如果tpl不存在 或者 模板文件的时间大于生成tpl文件的时间 就重新编译
if(!file_exists($compiledtplfile) || filemtime($templateFile)>filemtime($compiledtplfile)){
$this->template_compile($module,$template);
}
return $compiledtplfile;
}
/**
* 更新整个模块里面的模板
*
* @param unknown_type $module
* @return unknown
*/
function template_module($module)
{
$files = glob($this->template_file.'/'.$module.'/*.html');
if(is_array($files))
{
foreach($files as $tpl)
{
$tpl = split('/',$tpl);
$tpl = split('.html',$tpl[count($tpl)-1]);
$tpl = $tpl[0];
$this->template_compile($module, $tpl);
}
}
return TRUE;
}
/**
* 模板缓存
*
* @return unknown
*/
function template_cache()
{
foreach($MODULE as $module=>$m)
{
template_module($module);
}
return TRUE;
}
/**
* 模板解析
*
* @param string $str
* @return string
*/
function template_parse($str)
{
$str = preg_replace("/([\n\r]+)\t+/s","\\1",$str);
$str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str);
$str = preg_replace("/\{template\s+(.+)\}/","\n<?php include template(\\1); ?>\n",$str);
$str = preg_replace("/\{include\s+(.+)\}/","\n<?php include \\1; ?>\n",$str);
$str = preg_replace("/\{php\s+(.+)\}/","\n<?php \\1?>\n",$str);
$str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str);
$str = preg_replace("/\{else\}/","<?php } else { ?>",$str);
$str = preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$str);
$str = preg_replace("/\{\/if\}/","<?php } ?>",$str);
$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);
$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","\n<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str);
$str = preg_replace("/\{\/loop\}/","\n<?php } ?>\n",$str);
$str = preg_replace("/\{tag_([^}]+)\}/e", "get_tag('\\1')", $str);
$str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);
$str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);
$str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>",$str);
$str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "addquote('<?php echo \\1;?>')",$str);
$str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>",$str);
return $str;
}
}
//以下为外部函数,共模板调用
/**
* 将$var字符串,转换为可执行的php代码形式,并返回其结果..
* 转换为”,将为[xxx]转换为['xxx']
*
* @param unknown_type $var
* @return unknown
*/
function addquote($var)
{
return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}
/**
* 获取标签tag
*
* @param string $tagname
* @return unknown
*/
function get_tag($tagname)
{
if($tagname){
//print_r(explode('(',$tagname));
$tagname = str_replace('(','(array(',$tagname);
$tagname = str_replace(')','))',$tagname);
return "<?php echo {$tagname};?>";
}
}
?>
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码-雅虎php音乐 v1.0.zip
共13个文件
php:7个
html:4个
swf:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 158 浏览量
2022-10-19
02:48:17
上传
评论
收藏 30KB ZIP 举报
温馨提示
PHP实例开发源码—雅虎php音乐 v1.0.zip PHP实例开发源码—雅虎php音乐 v1.0.zip PHP实例开发源码—雅虎php音乐 v1.0.zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP实例开发源码—雅虎php音乐 v1.0.zip (13个子文件)
132699569251418411
so
temp.php 5KB
index.php 331B
2.php 333B
t.index.php 1KB
t1.index.php 351B
search.html 4KB
t.search.php 2KB
player.swf 11KB
template
music
search.html 5KB
search-30.html 4KB
searchb.html 4KB
template_cache
music_search.tpl.php 5KB
music_search.tpl.php.bak 5KB
共 13 条
- 1
资源评论
易小侠
- 粉丝: 6449
- 资源: 9万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功