智能模板引擎基础介绍
本文阐述了smarty模板引擎的基本知识。分享给大家参考。具体如下:
一.基本概念
1.什么是mvc?Mvc是一种开发模式,其核心思想是数据输入、数据处理和数据显示的强制分离。2.什么是聪明?Smarty是php的模板引擎。更具体地说,它可以帮助开发人员更好地将程序逻辑与页面显示分开。
3.smarty操作原理模板文件是用于显示数据的模板,其中要显示的数据被占位符替换。当smarty运行时,它将读取模板文件,用真实数据替换模板文件中的占位符,并输出一个经过处理的php文件供服务器运行。
第二,自己写一个聪明的模板
为了更好的理解smarty模板,你应该自己编写smarty模板——mini Marty,这样可以对smarty的操作原理有更深的理解。
1.新建项目minismarty新建模板文件路径:templates新建模板文件编译文件路径:templates_c新建模板文件:intro.tpl新建运行文件:index.php创建自己的smarty,即用于处理模板的文件:cls _ MiniSmarty.php
2.index.php文件的编写
?php需要_ once’。/cls _ minimarty . PHP ';$ minimarty=new minimarty();//传递数据$ mini smarty-assign ('title ','你好mini smarty!');$ minimarty-assign(' content ',' font color='red '这是内容!/font ');//发送数据到哪个页面显示$ minimarty-display(' intro . TPL ');3.编写intro.tpl文件
!-这是一个模板文件-html head meta http-equiv=' content-language ' content=' en '/meta name=' generator ' content=' phpeclipse 1.0 '/meta http-equiv=' content-type ' content=' text/html;charset=iso-8859-1 '/title { $ title }/title/head body bgcolor=' # ffffff ' text=' # 00000 ' link=' # ff 9966 ' vlink=' # ff 9966 ' alink=' # ffc 99 ' { content $这样就可以强制分离模板文件和数据文件,通过smarty传输数据。
编写cls _ MiniSmarty.php文件
?Php /** * *最初通过smarty模板引擎为模板提供数据*现在模仿并编写一个模板,该模板为模板提供数据*当Smarty运行时,读取模板文件并用可运行的Php文件替换它*服务器实际运行的文件是已处理的文件*/class minimarty {//模板文件路径var $template_dir='。/templates/'//替换模板文件后的文件路径为var $ templates _ c _ dir='。/templates _ c/';//存储变量值var $ TPL _ vars=array();//主要模拟2种方法/* * *添加数据*参数1: key *参数2: value,默认值为null */functionassign ($ TPL _ var,$ var=null) {if ($ TPL _ var!=' '){ $ this-TPL _ vars[$ TPL _ var]=$ var;//将数据添加到数组}}/* * *显示数据*参数1:要显示哪个模板文件*/函数display($ TPL _ file){//获取模板文件$ TPL _ file _ path的路径=$ this-template _ dir。$ TPL _ file//获取文件路径$ compile _ file _ path=$ this-templates _ c _ dir。com _ '。$ TPL _ file。模板文件编译后的“PHP”;//判断是否if(!file _ exists($ TPL _ file _ path)){ return false;}//不需要每次都生成编译文件,只需要编译文件不存在或者模板文件被修改即可。//相当于缓存编译文件。//filemtime函数:获取文件的生成时间if(!file _ exists($ compile _ file _ path)| | file mtime($ TPL _ file _ path)file mtime($ compile _ file _ path){//读取模板文件的内容$ FPL _ file _ content=file _ get _ contents($ TPL _ file _ path)$ newStr=myReplace($ fpl _ file _ content);//将替换后的字符串生成一个新文件,即编译后的文件file _ put _ contents($ compile _ file _ path,$ newstr);}//引入编译文件包含$ compile _ file _ path}/* * *替换模板文件中的内容以获得新的字符串*/函数my Replace($ fpl _ file _ content){ $ pattern=array('/\ { s * \ $([a-za-z _][a-za-z0-9 _]*)。$replace=array('?php echo $this-tpl_vars['${1}']?' );$newStr=preg_replace($pattern,$replace,$ fpl _ file _ content);返回$ newStr} } ?preg_replace方法介绍:参数1:替换规则参数2:替换内容参数3:替换操作内容
5.操作结果
显示标题和内容:
结论:
真正的运行文件既不是index.php也不是intro.tpl.php,而是smarty制作的:com_intro.tpl.php。这个文件中的数据来自index.php,显示的布局来自intro.tpl,中间的桥是smarty。smarty的作用是接受数据,填充数据(替换模板中的占位符),并加载替换的文件。
第三,解释smarty使用的细节
1.如何配置smarty?
解压缩后,将libs文件夹复制到项目目录,然后创建两个文件夹,templates和templates_c,分别放模板文件和模板编译文件。
2.使用smarty的注意事项
替换变量的标记。由于默认标识为{},在样式上与{}冲突,需要修改默认标识,一般修改为:{} 修改标识的方法。方法一:直接修改smarty类源代码:不推荐。方法二:使用smarty提供的方法进行修改。
$ smarty-left _ delimiter=“{”;$ smarty-right _ delimiter=“}”;智能的一些基本配置
$smarty-template_dir='。/templates ';//模板路径$ smarty-compile _ dir='。/templates _ c ';//编译路径$ smarty-cache=false;//是否使用cache $ smarty-cache _ dir='。/smarty _ cache ';//如果使用缓存:缓存3的路径。3.smarty模板技术分配变量的细节
底线:你可以分发php支持的各种数据。Php基本数据:int双字符串bool复合数据类型:数组对象特殊数据类型:resource null
希望这篇文章对大家的php编程有所帮助。
版权声明:智能模板引擎基础介绍是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。