自定义模板引擎
来源:互联网 发布:苹果cms采集规则教程 编辑:程序博客网 时间:2024/06/15 22:37
类文件my/config.class.php
<?php
classSmarty
{
//属性
public$arr;
//方法 向模板中分配变量
public function assign($name,$value)
{
$this->arr[$name]=$value;
}
//调用模板
public function display($temp)
{
//模板文件路径
$path="templaces/";
//编译文件路径
$com="templaces_c/";
//模板文件名字
$comfile=$com.$temp.'.php';
/*1.如果编译文件不存在需要重新生成编译文件
2.如果模板文件发生了改变,需要重新生成编译文件
*/
if(!file_exists($comfile)||filectime($path.$temp)>filectime($comfile)){//判断模板文件是否存在(如果文件不存在就创建)
//获取模板源代码
$str=file_get_contents($path.$temp);
//查找模板中的变量(模糊匹配)
//正则----$reg = '/\{\s*\$([a-zA-Z_]\w*)\s*\}/';
$reg='/\{\s*\$([a-zA-Z_]\w*)\s*\}/';
/*替换成<?php echo $this->arr['']?> 模式单元双引号加\\*/
$place="<?php echo\$this->arr['\\1'] ?>";
//获取替换后的代码
$html=preg_replace($reg,$place,$str);
//编译文件(模板文件.php)
file_put_contents($comfile,$html);
}
include_once$comfile;
}
}
模板文件my/templaces
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>Title</title>
<body>
<!--smarty模板引擎 引用变量:{变量}-->
{$title} {$hello}
</body>
</html>
<!--模板文件(可以是任何后缀)-->
引用文件my/templaces_c
<?php
header("Content-type:text/html;charset=utf-8");
/**
* Created by PhpStorm.
* User: lanouhn
* Date: 2017/10/12
* Time: 11:46
*/
include_once'config/smarty.class.php';
$smarty=newsmarty();
$title='只有学习让我快乐';
$content='我爱PHP';
$aa='我爱PHP';
//向模板中分配变量
$smarty->assign('hello',$title);
$smarty->assign('content',$content);
$smarty->assign('haha',$aa);
$smarty->display('01.html');
编译文件0.1.php
<?php
header("Content-type:text/html;charset=utf-8");
/**
* Created by PhpStorm.
* User: lanouhn
* Date: 2017/10/12
* Time: 11:46
*/
include_once'config/smarty.class.php';
$smarty=newsmarty();
$title='只有学习让我快乐';
$content='我爱PHP';
$aa='我爱PHP';
//向模板中分配变量
$smarty->assign('hello',$title);
$smarty->assign('content',$content);
$smarty->assign('haha',$aa);
$smarty->display('01.html');
阅读全文
0 0
- smarty自定义引擎模板
- 自定义模板引擎
- 自定义模板引擎_1
- 自定义模板引擎_2
- 自定义模板引擎_3
- 自定义模板引擎_4
- 自定义php模板引擎
- PHP自定义模板引擎
- 自定义模板引擎
- 自定义一个模板引擎
- 自定义Smarty模板引擎练习
- php简单的自定义模板引擎类。
- smarty模板引擎_5-自定义变量调节器
- smarty模板引擎_7-自定义函数
- smarty模板引擎_5-自定义变量调节器
- smarty模板引擎_7-自定义函数
- Express中使用自定义视图模板引擎
- play framework 之 对 模板引擎的 自定义扩展
- 缓存管理工具类
- neuq-oj-1042
- AtCoder Beginner Contest 075 总结
- 异步消息处理机制(一)
- windows下python2和python3共存
- 自定义模板引擎
- Android Studio的设备连接后设备名后为[null]
- 【Spring】Spring与Hibernate整合(十六)
- nginx常用命令
- maven配置jdk插件
- Android自定义View实现下拉刷新控件
- Object.keys()方法的使用
- postgresql 10 pg logical replication
- SQLyog 激活 注册码