仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2301|回复: 20
打印 上一主题 下一主题

[学习教程] PHP网站制作之从康盛产物(discuz)提掏出来的模板类

[复制链接]
飘飘悠悠 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:37:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
PHP和HTML混合编程应该不成问题,在这期间,你完全可以让PHP给你算算 一加一等于几,然后在浏览器输出,不要觉得幼稚,这的确是跟阿波罗登月一样,你打的是一小段代码,但是对于你的编程之路,可是迈出了一大步啊!兴奋吧?但是不得不再给你泼点冷水,您还是菜鸟一个。   复制代码 代码以下:
<?php
/*template.class.php
@康盛微博 模板提取类 感觉这个模板好用 花些工夫自力出来。 by 雷日锦
@看了一下ctt 这个模板 跟 phpcms的模板相似 岂非?? ^_^ 嘿嘿!!!
@ 微博 http://weibo.com/lrjxgl
@ 好器材人人同享 磕磕绊绊的提掏出来 有成绩请提出来
@ 模板文件默许为 .htm
$tpl = new template('skin',"default");
$tpl->objdir='tpp';
$tpl->rewrite=true;//开启rewrite 需求办事器撑持
$tpl->rewrite_rule=array(array("/index\.php/"),array("index.html")); //rewrite划定规矩
$tpl->assign("indexurl","index.php");
$tpl->assign("str","我是字符串啦啦啦");
$tpl->assign("ec","我是被echo出来的");
$tpl->assign("subhtml","{subtpl ttt}这是用来引入一个模板文件的,这个就是引入ttt.htm");
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc'));
$tpl->assign("i",1);
$tpl->display("index");
*/
if(!defined("CHARSET")) define("CHARSET","gb2312");//字符编码
if(!defined("DIR_TPL")) define("DIR_TPL","tpl");//默许模板目次
if(!defined("DIR_DATA")) define("DIR_DATA","data");//默许数据目次
if(!defined("DEBUG")) define("DEBUG",0);//默许运转形式
class template {
//note var
public $rewrite=false;//是不是开启 伪静态 rewrite
public $rewrite_rule=array(); //设置伪静态划定规矩
public $defaulttpldir;//默许的模板
public $tpldir;//模板目次
public $objdir;//编译缓存目次
public $tplfile;//模板文件
public $objfile;//编译文件
public $tplid=1;//模板编号
public $currdir='default';//以后作风目次
public $vars=array();//note 变量表
public $removeblanks=false;//移除空格
public $stdout='display';//输入类型
function __construct($tplid, $currdir) {
$this->template($tplid, $currdir);
}
function template($tplid, $currdir) {
ob_start();
if(file_exists(DIR_TPL.'/'.$currdir)) {
$this->currdir = $currdir;
$this->tplid = $tplid;
} else {
$this->currdir = 'default';
$this->tplid = 1;
}
$this->defaulttpldir = DIR_TPL.'/default';
$this->tpldir = DIR_TPL.'/'.$this->currdir;
$this->objdir = DIR_DATA.'/cache/tpl';
if(version_compare(PHP_VERSION, '5') == -1) {
register_shutdown_function(array(&$this, '__destruct'));
}
}
//note publlic
function assign($k, $v) {
$this->vars[$k] = $v;
}
//note publlic
function display($file) {
extract($this->vars, EXTR_SKIP);
include $this->getObj($file);
}
function getObj($file, $tpldir = '') {
$subdir = ($pos = strpos($file, '/')) === false ? '' : substr($file, 0, $pos);
$file = $subdir ? substr($file, $pos + 1) : $file;
$this->tplfile = ($tpldir ? $tpldir : $this->tpldir).'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
$this->objfile = $this->objdir.'/'.($tpldir ? '' : $this->tplid.'_').($subdir ? $subdir.'_' : '').$file.'.php';
//note 默许目次
if(@filemtime($this->tplfile) === FALSE) {
$this->tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
}
//note 判别是不是对照过时
if(!file_exists($this->objfile) DEBUG && @filemtime($this->objfile) < filemtime($this->tplfile)) {
$this->compile();
}
return $this->objfile;
}
function getTpl($file) {
$subdir = ($pos = strpos($file, '/')) === false ? '' : substr($file, 0, $pos);
$file = $subdir ? substr($file, $pos + 1) : $file;
$tplfile = $this->tpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
if(@filemtime($tplfile) === FALSE) {
$tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
}
return $tplfile;
}
function compile() {
$var_regexp = "\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*";
$vtag_regexp = "\<\?=(\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*)\?\>";
$const_regexp = "\{([\w]+)\}";
$template = file_get_contents($this->tplfile);
for($i = 1; $i <= 3; $i++) {
if(strpos($template, '{subtpl') !== FALSE) {
if(DEBUG == 2) {
$template = str_replace('{subtpl ', '{tpl ', $template);
} else {
$template = preg_replace("/[\n\r\t]*\{subtpl\s+([a-z0-9_:\/]+)\}[\n\r\t]*/ies", "file_get_contents(\$this->getTpl('\\1'))", $template);
}
}
}
$remove = array(
'/(^\r\n)\/\*.+?(\r\n)\*\/(\r\n)/is',
'/\/\/note.+?(\r\n)/i',
'/\/\/debug.+?(\r\n)/i',
'/(^\r\n)(\s\t)+/',
'/(\r\n)/',
);
$this->removeblanks && $template = preg_replace($remove, '', $template);
$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
$template = preg_replace("/\{($var_regexp)\}/", "<?=\\1?>", $template);
$template = preg_replace("/\{($const_regexp)\}/", "<?=\\1?>", $template);
$template = preg_replace("/(?<!\<\?\=\\\\)$var_regexp/", "<?=\\0?>", $template);
$template = preg_replace("/\<\?=(\@?\\\$[a-zA-Z_]\w*)((\[[\\$\[\]\w]+\])+)\?\>/ies", "\$this->arrayindex('\\1', '\\2')", $template);
$template = preg_replace("/\{\{eval (.*?)\}\}/ies", "\$this->stripvtag('<? \\1?>')", $template);
$template = preg_replace("/\{eval (.*?)\}/ies", "\$this->stripvtag('<? \\1?>')", $template);
$template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtag('<? echo \\1; ?>','')", $template);
$template = preg_replace("/\{for (.*?)\}/ies", "\$this->stripvtag('<? for(\\1) {?>')", $template);
$template = preg_replace("/\{elseif\s+(.+?)\}/ies", "\$this->stripvtag('<? } elseif(\\1) { ?>')", $template);
for($i=0; $i<2; $i++) {
$template = preg_replace("/\{loop\s+$vtag_regexp\s+$vtag_regexp\s+$vtag_regexp\}(.+?)\{\/loop\}/ies", "\$this->loopsection('\\1', '\\2', '\\3', '\\4')", $template);
$template = preg_replace("/\{loop\s+$vtag_regexp\s+$vtag_regexp\}(.+?)\{\/loop\}/ies", "\$this->loopsection('\\1', '', '\\2', '\\3')", $template);
}
$template = preg_replace("/\{if\s+(.+?)\}/ies", "\$this->stripvtag('<? if(\\1) { ?>')", $template);
$template = preg_replace("/\{tpl\s+(\w+?)\}/is", "<? include \$this->getObj(\"\\1\");?>", $template);
$template = preg_replace("/\{tpl\s+(.+?)\}/ise", "\$this->stripvtag('<? include \$this->getObj(\"\\1\"); ?>')", $template);
$template = preg_replace("/\{tmptpl\s+(\w+?)\}/is", "<? include \$this->getObj(\"\\1\", \$this->objdir);?>", $template);
$template = preg_replace("/\{tmptpl\s+(.+?)\}/ise", "\$this->stripvtag('<? include \$this->getObj(\"\\1\", \$this->objdir); ?>')", $template);
$template = preg_replace("/\{else\}/is", "<? } else { ?>", $template);
$template = preg_replace("/\{\/if\}/is", "<? } ?>", $template);
$template = preg_replace("/\{\/for\}/is", "<? } ?>", $template);
$template = preg_replace("/$const_regexp/", "<?=\\1?>", $template);//note {else} 也合适常量格局,此处要注重前后按次
$template = preg_replace("/(\\\$[a-zA-Z_]\w+\[)([a-zA-Z_]\w+)\]/i", "\\1'\\2']", $template);
$fp = fopen($this->objfile, 'w');
fwrite($fp, $template);
fclose($fp);
}
function arrayindex($name, $items) {
$items = preg_replace("/\[([a-zA-Z_]\w*)\]/is", "['\\1']", $items);
return "<?=$name$items?>";
}
function stripvtag($s) {
$vtag_regexp = "\<\?=(\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*)\?\>";
return preg_replace("/$vtag_regexp/is", "\\1", str_replace("\\\"", '"', $s));
}
function loopsection($arr, $k, $v, $statement) {
$arr = $this->stripvtag($arr);
$k = $this->stripvtag($k);
$v = $this->stripvtag($v);
$statement = str_replace("\\\"", '"', $statement);
return $k ? "<? foreach((array)$arr as $k => $v) {?>$statement<?}?>" : "<? foreach((array)$arr as $v) {?>$statement<? } ?>";
}
function __destruct() {
$content = ob_get_contents();
//处置 rewrite
if($this->rewrite) {
$arr=$this->rewrite_rule;
$s=$arr[0];
$e=$arr[1];
$content=preg_replace($s,$e,$content);
}
ob_end_clean();
echo $content;
}
}
$tpl = new template('skin',"default");
$tpl->objdir='tpp';
$tpl->rewrite=true;//开启rewrite 需求办事器撑持
$tpl->rewrite_rule=array(array("/index\.php/"),array("index.html")); //rewrite划定规矩
$tpl->assign("indexurl","index.php");
$tpl->assign("str","我是字符串啦啦啦");
$tpl->assign("ec","我是被echo出来的");
$tpl->assign("subhtml","{subtpl ttt}这是用来引入一个模板文件的,这个就是引入ttt.htm");
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc'));
$tpl->assign("i",1);
$tpl->display("index");
?>

新建 tpl/default/index.html
复制代码 代码以下:
<!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=gb2312" />
<title>无题目文档</title>
</head>

<body>
1.字符串赋值 :<br />
{$str}
<br />
2.数组赋值 :<br />
{loop $a $v}{$v},{/loop}
或<br />
{loop $a $key $val }{$val},{/loop}

3.{$subhtml}<br />
{subtpl ttt}<br />
4.本来我是{$indexurl } 如今我被酿成了index.php<br />
5.我还可以 echo 出来呢<br />
{echo $ec}<br />
6.其实我还可以加减乘除的 6*7*8
{echo 6*7*8;}
7.经常使用的就这些了 还有甚么不懂的 <br />

</body>
</html>

新建 tpl/default/ttt.html
新建 tpp目次 ok了理解动态语言的概念,运做机制,熟悉PHP语法
因胸联盟 该用户已被删除
沙发
发表于 2015-2-4 03:56:05 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
金色的骷髅 该用户已被删除
板凳
发表于 2015-2-4 20:49:12 | 只看该作者
建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。
透明 该用户已被删除
地板
发表于 2015-2-10 08:40:46 | 只看该作者
写的比较杂,因为我也是个新手,不当至于大家多多指正。
山那边是海 该用户已被删除
5#
发表于 2015-2-14 21:15:01 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
莫相离 该用户已被删除
6#
发表于 2015-2-25 06:29:27 | 只看该作者
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
精灵巫婆 该用户已被删除
7#
发表于 2015-3-7 17:21:10 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
海妖 该用户已被删除
8#
发表于 2015-3-8 05:02:32 | 只看该作者
其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
再现理想 该用户已被删除
9#
发表于 2015-3-15 20:40:56 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
不帅 该用户已被删除
10#
发表于 2015-3-21 09:14:44 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
飘飘悠悠 该用户已被删除
11#
 楼主| 发表于 2015-3-23 07:44:54 | 只看该作者
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
冷月葬花魂 该用户已被删除
12#
发表于 2015-3-23 15:40:39 | 只看该作者
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
蒙在股里 该用户已被删除
13#
发表于 2015-3-25 09:51:34 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
爱飞 该用户已被删除
14#
发表于 2015-4-9 11:54:54 | 只看该作者
首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。
乐观 该用户已被删除
15#
发表于 2015-4-19 15:18:12 | 只看该作者
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
活着的死人 该用户已被删除
16#
发表于 2015-4-26 02:29:01 | 只看该作者
建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。
17#
发表于 2015-4-29 17:47:29 | 只看该作者
其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
只想知道 该用户已被删除
18#
发表于 2015-5-2 13:05:19 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
若相依 该用户已被删除
19#
发表于 2015-5-5 20:30:47 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
分手快乐 该用户已被删除
20#
发表于 2015-5-7 02:32:31 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-20 19:56

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表