您的位置:首页 > 编程语言 > PHP开发

php smarty整理(二)自定义Smarty模板

2017-08-27 16:05 453 查看
<?php

/*

 *@简单的Smarty的自定义模板

*/

class MyTpl(){

//模板中的变量
protected $tpl_vals=array();

/*
*@显示模板
*/
public function display($tpl){
$contents=file_get_contents($tpl);

        foreach ($this->tpl_vals as $key => $val) {

        $contents = str_replace("{\$".$key."}", $val, $contents);

        }

$complieFile='complie/'.md5($tpl).'.php';
file_put_contents($complieFile, $contents);
require_once $complieFile;
}

    /*

     *@显示数据

    */
public function assign($name,$value){
$this->tpl_vals[$name]=$value;
}

}

$tpl= new MyTpl();

$tpl->assign('test','this is a test');

$tpl->display('xxx.html');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  smarty