Smarty s02
保留变量 方便使用php
不用assign
{$smarty}
get
{$smarty.get.page}
session
{$smarty.session.user.name}
server
cookies
request
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
Smarty s02的更多相关文章
- Smarty的基本使用与总结
含义: Smarty是PHP的一个引擎模板,可以更好的进行逻辑与显示的分离,即我们常说的MVC,这个引擎的作用就是将C分离出来. 环境需求:PHP5.2或者更高版本 我使用的环境是:PHP5.3,wi ...
- ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案
好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...
- smarty使用
smarty-牛刀小试 smarty 初识 官网 http://www.smarty.net/ Smarty is a template engine for PHP(PHP模板引擎) smarty使 ...
- Smarty模版引擎的原理
Smarty是一个使用php写出来的模版引擎,用来将原本与html代码混杂在一起PHP代码逻辑分离,实现前后端分离. Smarty模板优点: 1. 速度:采用Smarty编写的程序可以获得最大速度的提 ...
- 12月15日下午Smarty模板函数
1.{$var=...} 这是{assign}函数的简写版,你可以直接赋值给模版,也可以为数组元素赋值. <{$a = 10}><!--赋值语句--> <{$a}> ...
- 12月15日smarty模板基本语法
smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...
- 12月13日上午Smarty模版原理
模板主要是用来让前端和后端分离的,前台页面只是一个前台页面,后台页面用php代码写逻辑,写完逻辑拿到前台显示. 一.写法 一般需要以下:写3个页面: 1.显示页面aa.html <!DOCTYP ...
- SMARTY模板中如何使用get,post,request,cookies,session,server变量
{$smarty}保留变量不需要从PHP脚本中分配,是可以在模板中直接访问的数组类型变量,通常被用于访问一些特殊的模板变量.例如,直接在模板中访问页面请求变量.获取访问模板时的时间戳.直接访问PHP中 ...
- 在新浪云SAE中使用smarty引擎模版
在新浪云上使用smarty时会发现又这样的错误信息: “SAE_Fatal_error: Uncaught exception 'SmartyException' with message 'unab ...
随机推荐
- OC基础--内存管理中的@property关键字以及其参数
在上一篇博客中整理的内存管理,管理类的代码量会感觉很大,而且如果对象多的话,感觉到代码有点冗余.下面就介绍Xcode中为我们自动生成内存管理代码的关键字@property 例如:在Person这个类中 ...
- maven初学(三) SNAPSHOT
使用场景: 通常一个大型软件是由多个模块组成的,不同的组使用相同应用的不同版本. 在开发阶段,可能经常需要修复bug和优化. 这种情况下就会导致其他组频繁更新代码和pom文件 SANPSHOT SNA ...
- Tomcat tomcat-users.xml详解
conf/tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rol ...
- 【CodeForces 602B】G - 一般水的题2-Approximating a Constant Range
Description When Xellos was doing a practice course in university, he once had to measure the intens ...
- int方法
代码 #int内部功能 name='Kamil.Liu' age=18 num=-11 print(dir(age)) print(age.bit_length())#返回表示当前数字占用的最少位数 ...
- 搭建一个springMVC项目以及遇到的问题
首先找到jar包(lz现在还在学习maven,以后回了,就用maven了,自己配置时,jar包不全就很容易到时搭建失败)
- [NOIP2010] 提高组 洛谷P1525 关押罪犯
刚才做并查集想到了这道以前做的题,干脆一并放上来 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可 ...
- c链表实现遇到的错误
想完成一个链表发现有错误,代码如下: //http://ac.jobdu.com/problem.php?pid=1511 //֮ǰÓÃlistʵÏֵ쬽ñÌìÊÔÒ»ÏÂÓÃstruct ...
- NSThread - (void)start vs java Thread implements Runnable
This method spawns the new thread and invokes the receiver’s main method on the new thread. If you i ...
- Android模拟器端口被占用问题的解决办法
一.问题描述 今天在Eclipse中运行Android项目时遇到"The connection to adb is down, and a severe error has occured& ...