Smarty学习一:框架初探

Smarty 介绍

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

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

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

主要文件结构:
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");

现有版本为最新发布版本

Demo下载:http://www.oschina.net/code/snippet_252112_19124

转载于:https://my.oschina.net/haigou20/blog/113164

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值