SMARTY
Smarty是一个使用PHP写出来的模板引擎它分离了代码和外在的内容
提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离,目的就是要使PHP程序员同前端人员分离。
使程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新修改页面不会影响到程序的程序逻辑。
例如:
混合方式:<?php
$title="smarty学习";
$content="smarty模板的介绍"
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<p><?php echo $content;?></p>
</body>
</html>
模板方式:
a.html文件:
<html>
<head><title><{ $title }></title></head>
<body>
<p><{ $content }></p>
</body>
</html>
b.php文件:
<?php
include("MyTpl.php");
$tpl = new MyTpl;
$title="smarty学习";
$content="smarty模板的介绍";
$tpl->assign($title,"nihao");
?>
MyTpl文件:
<?php
class MyTpl{
//声明$template_dir属性,保存模板文件所在路径
//声明$complile_dir属性,保存编译后文件所在路径
$template_dir="./templates";//模板文件所在路径
$complile_dir="./templates_c";//生成文件所在路径
$tpl_vars=array();
public function __construct($template_dir="./templates",$compile_dir="./templates_c"){
$this->template_dir=rtrim($template_dir,'/').'/';
$this->compile_dir=rtrim($compile_dir,'/').'/';
}
}
//如何实现将php文件中声明的变量 分配到html(tpl)文件
//assign()方法
function assign($tpl_var,$value=null){
if($tpl_var!="")
$this->tpl_vars[$tpl_var]=$value;
}
?>