smarty的了解
来源:互联网 发布:evernote 知乎 编辑:程序博客网 时间:2024/05/01 05:09
Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是要使PHP程序员同前端人员分离,使程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。
今天老师将了些smarty,我所理解的smarty是把原先的PHP代码和HTML代码分离,至于有什么好处现在还没有看到。
a.html 和平常的html有所区别 ,使用 <{}> 显示的PHP代码变量
<html> <head> <title> <{ $title }> </title> </head> <body> <i><{ $content }></i> </body></html>
b.php 调用smarty,并且显示结果
<?php//本文件使用模板类//首先包含该模板文件include("MyTpl.php");//创建模板对象 new$tpl = new MyTpl();$title="smarty学习";$content="smarty模板的介绍"; //分配变量$tpl->assign("title",$title);$tpl->assign("content",$content);//调用模板文件//$a =$tpl->tpl_replace(file_get_contents("./templates/a.html"));//echo $a;$tpl->display("a.html");?>
MyTpl.php smarty的模板
<?phpclass MyTpl{ /* 声明 $template_dir属性,保存模板文件所在路径 声明 $compile_dir属性,保存编译后文件所在路径$template_dir="./templates";$compile_dir="./templates_c";$tpl_vars=array(); *public private protected * * $tpl=new MyTpl("./templates/","./templates_c/"); * */public function __construct($template_dir="./templates",$compile_dir="./templates_c"){ $this->template_dir=rtrim($template_dir,'/').'/'; $this->compile_dir=rtrim($compile_dir,'/').'/'; $this->tpl_vars=array();}/**如何实现将php文件中声明的变量 分配到html(tpl)文件 * assign()* 需要两个参数$tpl_var,$value* $tpl_var------出现在模板文件(*.html、 *.tpl)中变量的名称* $value--------模板文件中对应变量的值,来自于php文件*/function assign($tpl_var,$value=null){ if($tpl_var!=""){ $this->tpl_vars[$tpl_var]=$value; }}/*实现模板文件的调用*display("模板文件名")*第一步:从模板文件中获取 <{$titlename}> 结构*第二步:替换成<?php echo $titlename;?> 语法*///$content---整个模板文件//功能:从指定的模板文件中获取所有的 <{$titlename}>结构,全部替换成<?php echo $titlename;?〉function tpl_replace($content){ /* preg_replace(); * preg_replace ( 正则表达式,替换成,被替换对象 ) * */ //定义模板文件中<{ $title }>结构的正则表达式 $pattern = '/\<\{\s*\$([a-zA-Z_\x7f-\xff][0-9a-zA-Z_\x7f-\xff]*)\s*\}\>/i'; //替换后:<?php echo $title;? > 格式 //本文件中保存变量采取 $this->tpl_vars[title]=$value; //注意正则表达式中()的作用,参见第九章 $replacement= '<?php echo $this->tpl_vars["${1}"] ;?>'; //替换后返回一个新的html文件$repcontent = preg_replace($pattern,$replacement,$content);return $repcontent;}//将tpl_replace方法里返回的新文件保存到templates_c下//$fileName--*.html 或者 *.tplfunction display($fileName){ //$this->template_dir="./templates"; //$fileName="a.html"; //$this->template_dir.$fileName= "./templates/a.html" $tplFile=$this->template_dir.$fileName; //file_get_contents() $repcontent = $this->tpl_replace(file_get_contents($tplFile)); //将该编译后文件存储到templates_c里 com_****.php //将编译后的文件保存到templates_c,目前编译后文件为空 //pathname() basename() dirname() pathinfo() // ./templates_c/com_a.php $comFileName = $this->compile_dir."com_".basename($tplFile).".php"; //将变量$repcontent写入到 com_a.php 文件 $handle=fopen($comFileName,"w+"); fwrite($handle,$repcontent); fclose($handle); include($comFileName);}}?>
- smarty的了解
- 了解smarty自定义函数
- 什么是smarty,Smarty的优点?
- smarty 的注意事项和smarty的安装
- Smarty中文手册,Smarty教程,Smarty模板的入门教材
- Smarty中文手册,Smarty教程,Smarty模板的入门教材
- Smarty的基本配置【smarty.inc.php】
- 【smarty】smarty保留变量的使用
- Smarty入门教程一----Smarty的安装
- 【Smarty】Smarty的下载、配置与Helloworld
- smarty的程序设计部分
- Smarty的分页实现
- Smarty的分页实现
- Smarty的分页实现
- smarty 的判断符号
- smarty的简单分页
- 今天的smarty心得
- 配制Smarty的子类
- 第二种方式实现异步通信
- 浅析堆和栈
- beanstalk通讯协议
- 虚拟路径
- 带属性的自定义标签
- smarty的了解
- 控件的常用属性
- servlet应用
- 金额转为大写中文方法
- 把阿拉伯转换成汉字的方法
- 简单自定义标签的使用案例
- XML定义
- DOM4J实例
- 对Servlet的初始化的理解