Smarty学习一:框架初探

来源:互联网 发布:上海普旭网络 编辑:程序博客网 时间:2024/06/06 00:31
Smarty


使用php编写的模板引擎,用于将功能的逻辑代码和显示页面分离开来。程序执行时,smarty将php代码和html文件整合成以后编译成php文件放在指定目录下。


特点:
功能简单,开发快
速度快,发展时间较长,技术上相对更稳定一些
缓存技术,减轻服务器压力
便于扩展,可自定义插件。


不适合场景:
实时更新,如微波\股票之类
小项目


主要文件结构:
libs/ smarty类文件
smarty类
demo/ 使用教程


::::::::::::::配置安装,使用实例:::::::::::::::::
require('libsmarty/Smarty.class.php');$tpl = new Smarty;$tpl->caching = false; //是否缓存,一般开发时设为false , 默认关闭$tpl->template_dir = "./templates"; //模板目录$tpl->compile_dir = "./templates_c"; //必须项,编译目录,模板被编译后的目录$tpl->cache_dir = "./smarty_cache"; //可选项//设置模板变量,tpl_var为模板变量名,tpl_value为变量值(可为字符串、数字、数组、对象等)//$tpl->assign("tpl_var" , "tpl_value");//获取模板,index.html为模板名称,位于./templates/index.html//$tpl->display("tpl_name");//使用示例//模板文件内容:<h1>{$hello}</h1> hello为assign指定的变量名,将内容"Hello World"显示在模板上$tpl->assign("hello" , "Hello World"); //设置模板变量//遍历数组//在模板中使用smarty内置方法section/***** 模板中的设置** {section name=s loop=$items}**  s为循环名称,loop为循环数据,$tpl->assign('items',$items)** {$stu[s].name}** {sectionelse}** 无循环结束** {/section} --循环结束***/$items = array();$items[] = array('name'=>'apple');$items[] = array('name'=>'orange');$items[] = array('name'=>'banana');$tpl->assign('items' , $items); //定义数组名,并赋值//获取模板,index.html为模板名称,位于./templates/index.html$tpl->display("index.html");

现有版本为最新发布版本