PHP模板引擎
该文档使用的是Smarty 3.1.33 Released版本
什么是PHP模板引擎?
- php模板引擎是将php代码和前端代码剥离的工具,
- 一般是时使用php语言进行编写
- 模板引擎文件中大部分代码是前端的代码,只有一部分涉及到php的代码会用一些特殊的标签替代
- 学习模板引擎其实就是在学习这些特殊的标记标签
什么是Smarty?
- smarty是一个使用php编写的php模板引擎,是众多php的模板和引擎中使用最为广泛的一种.
Smarty的配置
smart配置左右定界符
- smarty默认的定界符为
{}
- 可以通过以下代码配置自定义定界符为
{{}}
$smarty = new Smarty();
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';
smarty自定义模板文件目录
- 默认情况下,模板文件路径为
./templates
,模板编译文件路径为./templates_c
,缓存文件目录为./cache
- 可以通过以下代码自定义模板路径 , 编译文件路径, 缓存路径
//设置模板文件目录
$smarty->setTemplateDir($dir)
//$smarty->getTemplateDir();$template_dir;
// 设置编译目录
$smarty->setCompileDir('./templates_c');
//$smarty->getCompleDir();$compile_dir;
// 设置缓存目录
$smarty->setCacheDir('./cache');
//$smarty->getCacheDir();$cache_dir;
// 方法连用
$smarty->setTemplateDir('./templates')
->setCompileDir('./templates_c')
->setCacheDir('./cache');
Smarty的使用
内置函数(模板页面常用标签)
{foreach}
- 作用: 循环遍历数组
- 使用示例
{$data = [1,2,3,4,5]}
{foreach $data as $value}
{$key}-{$value}
{/foreach}
{php}
- 作用: 用于书写原生php代码
- 示例代码
{php}
$arr=array('name'=>'zs','age'=>19);
foreach($arr as $key=>$val){
echo $key.'='.$val;
}
{/php}
{include}
- 作用: 在当前模板文件中包含另外一个模板文件
- 示例代码:
- 包含文件
// 引入page_header.tpl文件 {include file='page_header.tpl'}
- 传递变量
- a.tpl
{include 'b.tpl' title='我是a模板传递过来的变量'}
- b.tpl
<h1>{$title}</h1>
{if} {elseif} {else}
- 作用: 逻辑判断
- 示例代码
{if isset($name) && $name == 'Blog'}
{* do something *}
{elseif $name == $foo}
{* do something *}
{/if}
{if is_array($foo) && count($foo) > 0}
{* do a foreach loop *}
{/if}
{literal}
- 作用: 防止代码被解析(写在这个标签内部的代码不会被smarty解析,一般用于防止js代码被smarty解析)
- 示例代码
<script>
{literal}
function myBar {alert('Bar!');}
{/literal}
</script>
{**}
- 作用: 书写注释
- 示例代码
{*这是一段smarty注释*}
{assign}
- 作用: 用于在模板运行时,赋值变量
- 示例代码
{assign var="name" value="Bob"}
//{$name}会输出'Bob'
{$name}
变量修饰符
- 变量修饰符就是在模板页面的变量后面,写一个
|
,在|
的后面写一个函数名,该函数可以使系统函数,也可以是自定义函数 - 使用
:
将函数的参数和函数名隔开,如果有多个参数,也是使用:
分割
常用系统变量修饰符
default
- 作用:给变量设置默认值
- 示例代码
{$name|default='暂无姓名'}
truncate
- 作用: 截取字符串
- 示例代码
{assign var='title' value='这是一个很长的Title,长到会撑破你的网页结构,你怎么办?'}
//这是一个很长的T
{$title|truncate:8:}
//这是一个很长的标T...
{$title|truncate:8:"..."}
//第三个参数表示按照单词截取,按照指定长度截取的同时,会保证单词的完整性不被破坏
//这是一个很长的标Title...
{$title|truncate:8:"...":true}
strip_tags
- 作用: 删除(过滤)字符串中标签
- 示例代码
{assign var='content' value='<h1>我是h1标题</h1>'}
{$articleTitle|strip_tags}
nl2br
- 作用: 将字符串中的
\n
替换成<br/>
- 示例代码
//php页面
<?php
$smarty->assign('title',
"Sun or rain expected\ntoday, dark tonight"
);
?>
//模板页面
{$title|nl2br}
lower
- 作用: 将字母全部转成小写
- 示例代码
{assign var='title' value="THIS IS A TITLE"}
{$title|lower}
upper
- 作用: 将字母全部转成大写
- 示例代码
{assign var='title' value="this is a title"}
{$title|upper}
自定义变量修饰符
- 模板中使用系统或者自定义函数作为变量修饰符
- 除了以上系统提供的变量修饰符之外,php系统函数或者一个自定义函数也可以作为变量修饰符;默认变量作为函数的第一个参数,如过函数有多个参数,使用
:
分割
{assign var='title' value='This is as title'}
//全部转成大写
{$title|strtoupper}
//全部转成小写
{$title|strtolower}
//计算字符串长度
{$title|strlen}
自定义函数(了解)
html_options
- 准备数据
<?php
$smarty->assign('myOptions', array(
1800 => 'Joe Schmoe',
9904 => 'Jack Smith',
2003 => 'Charlie Brown')
);
$smarty->assign('mySelect', 9904);
?>
- 模板调用
/*
name:为select标签最终的name
options:下拉框的选项
selected:默认选中项
*/
{html_options name=foo options=$myOptions selected=$mySelect}
- 输出
<select name="foo">
<option value="1800">Joe Schmoe</option>
<option value="9904" selected="selected">Jack Smith</option>
<option value="2003">Charlie Brown</option>
</select>
{html_table}
- 数据
<?php
$smarty->assign( 'data', array(1,2,3,4,5,6,7,8,9) );
$smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') );
$smarty->display('index.tpl');
?>
- 模板
{html_table loop=$data cols="first,second,third,fourth" tr_attr=$tr}
- 输出
<table border="1">
<thead>
<tr>
<th>first</th><th>second</th><th>third</th><th>fourth</th>
</tr>
</thead>
<tbody>
<tr bgcolor="#eeeeee"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr bgcolor="#dddddd"><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr bgcolor="#eeeeee"><td>9</td><td> </td><td> </td><td> </td></tr>
</tbody>
</table>
fetch
{* assign the fetched contents to a template variable *}
{fetch file='http://www.baidu.com/' assign='weather'}
<div id="weather">{$weather}</div>
缓存(了解)
开启缓存
<?php
require('Smarty.class.php');
$smarty = new Smarty;
// 使用$smarty->cacheLifetime()可以更精确定义缓存时间
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->display('index.tpl');
?>
设置缓存时间
<?php
require('Smarty.class.php');
$smarty = new Smarty;
// 让每个缓存的过期时间都可以在display执行前单独设置。
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);
// 设置index.tpl的过期时间为5分钟
$smarty->setCacheLifetime(300);
$smarty->display('index.tpl');
// 设置home.tpl的过期时间为1小时
$smarty->setCacheLifetime(3600);
$smarty->display('home.tpl');
// 注意:当$caching设置了Smarty::CACHING_LIFETIME_SAVED后,
// 下面的$cache_lifetime将不会起效。
// home.tpl已经设置了过期时间为1小时,
// 所以不会再遵循下面的$cache_lifetime值,
// home.tpl的过期时间还是1小时。
$smarty->setCacheLifetime(30); // 30 秒
$smarty->display('home.tpl');
?>
删除缓存
<?php
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
if(!$smarty->isCached('index.tpl')) {
// 找不到缓存,这里进行一些赋值操作
$contents = get_database_contents();
$smarty->assign($contents);
}
$smarty->display('index.tpl');
?>
网友评论