分享一个非常简单好用的php模板引擎
因为最近准备开发一套php分布式系统,采用主配置文件通用,每个模块遵守开发规范独立开发,独立部署到服务器上,整套系统只共用会员数据。
每个模块开发数据api 接口,供主程序调用数据报表做分析。
主要面向企业客户,用于企业采购、销售订单管理,仓储库存管理,企业财会,客户管理等等。
另外对外是b2b,面向客户运营,暂时可能对接其它的系统,减少开发量。
大家有好的架构建议可以提出来,因为我只有一个人开发,所以尽量简单的开发。以后慢慢完善。
具体怎么架构还在筹划中,下面是我在项目中刚刚写的一个php 模板引擎,喜欢的朋友可以拿去。
在htm 模板中使用方法
循环调用
{loop $lists $i $t}
<a href="{$t[linkurl]}">{$t[title]}</a>
{/loop}
直接调用变量
{$title}
php 文件引入函数 template($name = "index", $dir = ""); 即可
下面是引擎代码
<?php
function template($name = "index", $htmdir = "",$phpdir="") { //模板引入函数
$htmlfile = $htmldir."/".$name.".htm"; //静态文件地址
$phpfile = $phpdir."/".$name.".php"; //编译文件地址
if(!is_file($phpfile)){ //判断编译文件存在否
//调用模板编译函数对html 模板进行php编译
template_cache($htmlfile,$phpfile);
}
include $phpfile;
}
//构造一个模板编译函数
function template_cache($htmlfile, $phpfile) {
$content = template_replace(@file_get_contents($htmlfile)); //这里引入替换函数将htm中的标签替换成php
file_put_contents($phpfile, $content); //将静态替换好的文件保存到当前模型的缓存目录下
}
//htm 标签替换
function template_replace($str) {
$str = preg_replace("/<!--[(.+?)]-->/", "", $str);
$str = preg_replace("/<!--{(.+?)}-->/s", "{\1}", $str);
$str = preg_replace("/{templates+([^}]+)}/", "<?php include template(\1);?>", $str);
$str = preg_replace("/{phps+(.+)}/", "<?php \1?>", $str);
$str = preg_replace("/{ifs+(.+?)}/", "<?php if(\1) { ?>", $str);
$str = preg_replace("/{else}/", "<?php } else { ?>", $str);
$str = preg_replace("/{elseifs+(.+?)}/", "<?php } else if(\1) { ?>", $str);
$str = preg_replace("/{/if}/", "<?php } ?> ", $str);
$str = preg_replace("/{loops+(S+)s+(S+)}/", "<?php if(is_array(\1)) { foreach(\1 as \2) { ?>", $str);
$str = preg_replace("/{loops+(S+)s+(S+)s+(S+)}/", "<?php if(is_array(\1)) { foreach(\1 as \2 => \3) { ?>", $str);
$str = preg_replace("/{/loop}/", "<?php } } ?>", $str);
$str = preg_replace("/{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(([^{}]*)))}/", "<?php echo \1;?>", $str);
$str = preg_replace_callback("/<?php([^?]+)?>/s", "template_addquote1", $str);
$str = preg_replace("/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_+-\x7f-\xff]*)}/", "<?php echo \1;?>", $str);
$str = preg_replace_callback("/{(\$[a-zA-Z0-9_[]""$+-\x7f-\xff]+)}/s", "template_addquote2", $str);
$str = preg_replace("/{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)}/s", "<?php echo \1;?>", $str);
$str = preg_replace("/"([A-Za-z]+)["([A-Za-z.]+)"](.?)"/s", ""\1[\2]\3"", $str);
$str = preg_replace("/( ? )\1+/", "\1", $str);
$str = str_replace(" ", "", $str);
$str = "<?php defined("IS_HJCMS") or exit("非法访问");?>".trim($str);
return $str;
}
function template_addquote1($matches) {
return str_replace("\"", """, preg_replace("/[([a-zA-Z0-9_-.\x7f-\xff]+)]/s", "["\1"]", $matches[0]));
}
function template_addquote2($matches) {
return "<?php echo ".str_replace("\"", """, preg_replace("/[([a-zA-Z0-9_-.\x7f-\xff]+)]/s", "["\1"]", $matches[1])).";?>";
}
?>
可以封装成template.inc.php 在 项目的配置文件中引入
<?php
include "template.inc.php";
$htmdir = ""; //自己设置
$phpdir = "";//自己设置
template("login",$htmdir,$phpdir); //
?>
使用起来非常简单,比php的smarty 好用多了
- 上一篇:没有了
- 下一篇:没有了