|
1:smarty介绍及应用的优缺点
什么是smarty?
smarty 是一个使用php写出来的php模板引擎,目的是要使php程序同美工分离,使的程序员改变程序的逻辑内容是不会影响到美工的页面设计,美工重新修改页面时不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。
smarty的优点
1:速度快
2;编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的php文件
3:缓存技术:它可以将用户最终看到的html文件缓存为一个静待的html页
4:插件技术:smarty可以自定义插件
不适合使用smarty的地方
1:需要实时更新的内容
2:小项目。
2:smarty的配置
include_once("Smarty/Smarty.class.php"); //包含smarty类文件
$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->config_dir="Smarty/Config_File.class.php"; // 目录变量
$smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
$smarty->template_dir = "./templates"; //设置模板目录
$smarty->compile_dir = "./templates_c"; //设置编译目录
$smarty->cache_dir = "./smarty_cache"; //缓存文件夹
//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突
//----------------------------------------------------
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
3:smarty的应用:变量,循环。。。。
$smarty->assgin("模板变量","值(数组/变量)");
$smarty->display("模板名称");
例子:
include("");//引入smarty_inc.php文件
$smarty->assgin("name","php100");//进行模板变量的替换
$smarty->display("index.htm");//给文件是模板文件,应该在模板目录里
模板文件定义:
<html>
<title>{$name}</title>
</html>
$smarty->assgin("模板变量","数组");
数组是最常用的方式,可以帮助我们循环列表,和快速访问打理数据,如果要循环数组我们可以使用smarty内置的方法section
{section name=s loop=$stu}
{$stu.name}
{sectionelse}
无内容
{/section}
|
|
|