保留变量 方便使用phpphp
不用assignhtml
{$smarty}数据库
get缓存
{$smarty.get.page}cookie
sessionsession
{$smarty.session.user.name}app
server函数
cookies插件
requestorm
const(常量)
变量调节器
escape(跟php中的htmlentities())
$smarty->assign('title','<h3>标题</h3>')
{$title}则直接输出 html的格式
{$title|escape} 转码
escape($title)
default
当没有的时候则是默认值
date_format
{$smarty.now|date_format:"Y-m-d H:i:s"}
truncate(截取长度)
{"标题标题"|truncate:8}
upper(转大写)
{"lamp"|upper}
strtoupper
{"lamp"|strtoupper}
{"="|str_repeat:40}
php中的函数能够直接做为变量调节器使用
自定义函数也能够
内置函数
foreach
{foreach $stulist as $stu}
<tr>
<td>{$stu@index+1}</td>
<td>{$stu.name}</td>
</tr>
{/foreach}
if elseif else
while
include
for
(0-100的偶数)
{for $i=1 to 100 step 2}
{i}
{/for}
A. Smarty配置
定义定界符(属性)
$left_delimiter[左定界符]
$right_delimiter[右定界符]
$smarty->left_delimiter='<{';
$smarty->right_delimiter='}>';
定义模版目录
$template_dir[模板目录]
$smarty->template_dir='./view';//属性
$smarty->setTemplateDir('./view');//方法
定义编译的目录
$smarty->setCompileDir('./view_c');
编译目录不存在则本身建立
缓存目录
$smarty->setCachrDir('./cache') ;
配置文件的目录
$smarty->setConfigDir('./config');
B. Smarty缓存
$smarty->conpile_check [编译检查]
$smarty->conpile_compile [强制编译]
1.开启缓存
$caching
$smarty->caching=1;
接受参数
$id=intval($_GET['id'])l
//判断缓存是否有效,把文章ID做为缓存ID
//同一个模版,就能够有多个缓存文件
if(!smarty->isCached('article.tpl',$id)){
$data=array(
2=>array('id'=>2,'title')
)
}
2.缓存时间
$smarty->cache_lifetime=50 //秒表 默认是3600
3.//判断是否有缓存
if(!$smarty->isCached('1.tpl')){
//有多是从数据库查询
//分配变量
$smarty->assign('title','标题')
//当前时间
$smarty->assign('now',date('Y-m-d H:i:s'));
}
4.$smarty->display('article.tpl',$id)
不想缓存的地方
{nocache}
{$smarty.now|date_format}
{/nocache}
//清除某一个模版的缓存
$smarty->clearCache('article.tpl')
//清除某一个模版的ID的缓存
$smarty->clearCache('article.tpl',2)
C. Smarty插件
第一种方法
//将自定义函数注册为变量调节器
//第一个参数是插件的类型(9种)
//第二个参数是在Smarty中使用的名字
//第三个参数是咱们自定义的函数名字
$smarty->registerPlugin('modifier','wanghaoyu',myfun)
第二种方法
//添加一个插件目录
//将特定命名的插件放到目录中
//规则:类型前缀.函数名.php(9中类型前缀)
$smarty->addPluginDir('./myPlugins')
自定义函数的 smarty_modifier_前缀不能改
D. Smarty继承 extends
display('index.html')
在index代码中写上
{extends 'base.html'}
{block name='main'}
<div>
index的内容
</div>
{/block}
在base.html想改的区域
{block name='main'}
{/block}
惟一能作的就是重写某一块
2.在block里面追加内容
{block name='top' append}
<div>haha</div>
{/block}
3.某个部分的背景变色,当内容变的时候同时变
{block name='top'}
<div class='top'>
{$smarty.block.parent}
</div>
{/block}
4.title
模版
{block name='head'}
<head>
<title>{$smarty.block.child}</title>
</head>
{/block}
显示页
{block name='head'}文章{/block}
E. Smarty 的include