php-smarty模板使用教程(四)-程序员篇
Smarty程序设计篇
模板设计-à前台人员;程序设计-àphp开发人员 一、 Smarty模板常量 SMARTY_DIR Smarty框架的路径 代码:$smarty=new Smarty(); //在这里,我们输出了smarty框架的路径 echo SMARTY_DIR; 二、Smarty模板 变量 l $template_dir :模板路径 l $compile_dir :编译路径 l $config_dir :配置文件路径 l $cache_dir :缓存文件 以上四个属性在2.6是公有的,在3.0里有私有的,建议大家使用相应的set和get方法进行设置和读取 l $left_delimiter :左分隔 l $right_delimiter :右分隔符 以上二个属性是公有的,可以直接访问 这们的默认值是 “{“ “}” 为了不和css和js起冲突,一般情况下,程序员会将这两个属性的值设置为以下几种 <{$name}> {*$name*} l $caching :是否开启缓存 l $cache_lifetime :默认的缓存时间 如果想在smarty中使用缓存,都必须将当前caching的属性设置为true Caching在3.0中是公有的,可以直接设置,它的值。默认是false,表示不开启缓存功能 默认的缓存时间的单位是秒,值是3600(一小时) l $debugging 开启缓试窗口 在smarty中,开启调试共有2种方法: 1)在模板中使用 {debug} 2)在程序中使用 $smarty->debugging=true {debug}可以观察模板中所有的变量,包括模板创建的变量 $debugging属性打开的调试只能观察php文件分配的变量 l $php_handling 在smarty2.6中,如果想在模板中使用{php}{/php}这个函数,必须设置该属性的值为true,在3.0中,可以不设置 问题: 在smarty中,编译目录和缓存目录是不是自动生成的?答:是!!! Tip:不过不要过渡依赖这个自动生成,应该尽量手动创建! 如果模板的内容发生改变,smarty会重新生成缓存文件三、 Smarty模板方法 l assign :为模板变量赋值 (传值) l assignByRef :为模板变量赋值 (传地址) l append :将变量或值追加至数组 l appendByRef :将变量以地址形式追加至数组 append认为,数据是可变的,是不固定的 l clearAllAssign :清除所有已赋值的模板变量 l clearAssign :清除指定的模板变量 l clearCache :清除缓存 l configLoad :加载配置文件 在smarty中,如果想加载一个配置文件,有两种方式: 1) 在模板中 {config_load file=’file’} 2) 在程序中 $smarty->configLoad(配置文件名,配置节); l clearConfig :清除某个配置文件的变量 l display :显示某模板 1) 读取模板内容 2) 替换字符串 3) 输出 l fetch :返回替换之后的字符串 1) 读取模板内容 2) 替换字符串 3) 返回字符串(返回给php程序) l templateExists :判断模板是否存在 在smarty中,如果使用了一个不存在的模板则会出错,所以在显示一个模板之前应 该对这个模板进行判断,判断其是否存在 下面是代码部分:
- //要开启缓存,设置为true即可
- $smarty->caching=true;
- //这个缓存时间的设置一般取决于网站的流量,越大,时间越小,越小,时间越大
- $smarty->cache_lifetime=3600;
- $smarty->assign('name','lisi');
- //开启smarty的调试窗口
- $smarty->debugging=true;
- $smarty->display('demo01.htpl');
- //assignByRef 为模板变量赋 变量地址
- $name='xiaogang';
- //$smt->assign('name',$name);
- $smt->assignByRef('name',$name);
- $name="lixiaolong";
- $smt->display('demo2.html');
- /*append的用法、appendByRef的用法*/
- $c1='美国';
- $smarty->appendByRef('city',$c1); //将c1变量以地址形式传给city
- $smarty->append('city','北京'); //将北京追加模板变量数组city中
- $smarty->append('city','上海'); //将上海追加模板变量数组city中
- $c1='山东';
- //这种写法相当于
- // $smarty->assign('city',array('北京','上海'));
- //将所有之前模板变量的值清空
- //$smarty->clearAllAssign();
- //将模板变量name的值清空
- //$smarty->clearAssign('name');
- //$smarty->caching=true;
- //表示清除demo02.htpl这个模板文件的缓存文件
- //$smarty->clearCache('demo02.htpl');
- //加载demo02.conf配置文件
- //$smarty->configLoad('demo02.conf');
- //表示将配置文件中的fgcolor的值清除
- //$smarty->clearConfig('fgcolor');
Assign和assignByRef的区别: 四、 对象应用 l 注册对象到模板,然后通过类似于用户自定义函数的形式来访问它。 l 给模板分配对象,然后通过访问其它赋值变量类似的方法进行访问
- //fetch方法一般用于生成静态页
- // $str=$smarty->fetch('demo03.htpl');
- // $filename='html/'.date("YmdHis").'.html';
- // file_put_contents($filename, $str);
- //显示某个模板
- ///$smarty->display('demo03.htpl');
- //调用了一个不存在的模板,程序出错
- //如果模板存在,返回值为true,反之为false
- if($smarty->templateExists('demo04.htpl'))
- $smarty->display('demo04.htpl');
- else
- echo '模板不存在!';
五、 过滤器 就是用来过滤数据的 1、过滤器的种类 Prefilters预过滤器 Postfilters后过滤器 Output Filters输出过滤器(输出滤镜) 2、过滤器的流程 l tpl源文件 =〉Prefilter =〉编译tpl文件 => Postfilter =>保存到磁盘=> 编译过的php文件执行=〉Output Filters(=〉如果有smarty cache的话,Output Filters的内容会缓存) =>结果输出。 读取tpl模板文件之后,对它进行编译之前,触发的是预过滤器 编译之后,保存之前,触发的是后过滤器 保存之后,输出之前,触发的是输出过滤器 3、使用过滤器 在smarty中,使用过滤器必须要先注册 在smarty2.6中,使用过滤器通过以下形式: l Prefilters注册预过滤器 $smarty->register_prefilter(“func”); l Postfilters 注册后过滤器 $smarty->register_postfilter(“func”); l Output Filters 注册输出过滤器 $smarty->register_outputfilter(“func”); 在smarty3.0中,使用以下形式注册: $smarty->registerFilter($type, $callback) Type:过滤器类型 取值范围:pre、post、output Callback:回调函数 (当触发过滤器时执行的函数) 4、开始使用
- class Person
- {
- public $name;
- public $age;
- public function display()
- {
- echo '你好,我的名字是'.$this->name.'年龄是'.$this->age.'
';- }
- }
- $per=new Person();
- $per->name='zhangsan';
- $per->age='30';
- include('../Smarty/Smarty.class.php');
- $smarty=new Smarty();
- $smarty->assign('per',$per); //为模板分配了一个对象per
- $smarty->display('demo04.htpl');
预过滤器和后过滤器只要模板没有改变,只能执行一次 输出过滤器可以每次执行
- //display1是预过滤器的回调函数
- function display1($tpl) //tpl表示模板内容
- {
- echo '这是预过滤器!
';- //$tpl=str_replace('#title#', '家教网', $tpl);
- return $tpl;
- }
- function display2($tpl)
- {
- echo '这是后过滤器!
';- //$tpl=''.$tpl;
- return $tpl;
- }
- function display3($tpl)
- {
- echo '这是输出过滤器!
';- $tpl=str_replace('smarty', 'smarty', $tpl);
- return $tpl;
- }
- include('../Smarty/Smarty.class.php');
- $smarty=new Smarty();
- //注册预过滤器
- $smarty->registerFilter('pre', 'display1');
- //注册后过滤器
- $smarty->registerFilter('post', 'display2');
- //注册输出过滤器
- $smarty->registerFilter('output', 'display3');
- $smarty->display('demo05.htpl');
PHP之友评论