Smary模板应用30分钟入门教程

来源:互联网 发布:淘宝购物车营销针对 编辑:程序博客网 时间:2024/04/26 17:03

〇. 引言
在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。现在就用30分钟让您快速入门。

一. 安装
首先打开网页http://smarty.php.net/download.php,下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来我演示给大家一个安装实例,看过应该会举一反三的。
(1) 我在根目录下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, config/,如下图:
[img=165 border=0,170 src=]http://www.phpchina.com/bbs/[/img]
(2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" c>
<title>Smarty</title>
</head>
<body>
{$hello}
</body>
</html>
新建index.php,将此文件放在learn/下:
<?php
//引用类文件
require 'smarty/libs/Smarty.class.php';

$smarty = new Smarty;

//设置各个目录的路径,这里是安装的重点
$smarty->template_dir ="smarty/templates/templates";
$smarty->compile_dir ="smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir ="smarty/templates/cache";

//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching = false;

$hello = "Hello World!";
//赋值
$smarty->assign("hello",$hello);

//引用模板文件
$smarty->display('index.tpl');

?>

(3) 执行index.php就能看到Hello World!了。


二. 赋值
在模板文件中需要替换的值用大括号{}括起来,值的前面还要加$号。例如{$hello}。这里可以是数组,比如{$hello.item1},{$hello.item2}…
而PHP源文件中只需要一个简单的函数assign(var , value)。
简单的例子:
*.tpl:
Hello,{$exp.name}!Good {$exp.time}
*.php:
$hello[name]= “Mr. Green”;
$hello[time]=”morning”;
$smarty->assign(“exp”,$hello);

output:
Hello,Mr.Green!Good morning

三. 引用
网站中的网页一般header和footer是可以共用的,所以只要在每个tpl中引用它们就可以了。
示例:*.tpl:
{include file="header.tpl"}

{* body of template goes here *}

{include file="footer.tpl"}

四. 判断
模板文件中可以使用if else等判断语句,即可以将一些逻辑程序放在模板里。"eq","ne", "neq", "gt", "lt","lte", "le", "gte" "ge","is even", "is odd", "is not even", "is notodd", "not", "mod", "div by", "evenby", "odd by","==","!=",">","<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。
示例:
{if $name eq"Fred"}
WelcomeSir.
{elseif $name eq"Wilma"}
WelcomeMa'am.

{else}
Welcome,whatever you are.
{/if}


五. 循环
在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。
示例:
{* this examplewill print out all the values of the $custid array *}
{secti loop=$custid}
id: {$custid[customer]}<br>
{/section}

OUTPUT:

id: 1000<br>
id: 1001<br>
id: 1002<br>

六. 常见问题
Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascrīpt函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。
示例:
{literal}
<scrīptlanguage=javascrīpt>
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
</scrīpt>
{/literal}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子不喜欢和小朋友玩怎么办 孩子不喜欢和小朋友说话怎么办 4岁半宝宝不听话怎么办 小孩在学校打老师怎么办 老师老找孩子时怎么办 幼儿园老师批评孩子后家长怎么办 老师跟家长吵架了怎么办 孩子在幼儿园被老师孤立怎么办 学生在幼儿园被老师欺负怎么办 小孩脚痒怎么办小窍门 小孩肚子病怎么办天天说 幼儿园幼儿信息表填错了怎么办 水浒传书孩子说看不懂怎么办 孩子丢了书老师怎么办 小朋友做错事不承认老师怎么办 教师被学生骂后怎么办 嘴吧里面长泡怎么办 有个小孩怕下雨怎么办? 幼儿的家长打我怎么办 老师打学生被家长起诉怎么办 家长在学校打了老师怎么办 老师打小孩我们家长怎么办呢? 孩子长手、腿毛怎么办 腿毛又黑又多怎么办 孩子怕老师说他怎么办 被老师骂到厌学怎么办 孩子对写作业一点也不主动怎么办 高三孩子太贪玩怎么办 玩心重的孩子该怎么办 儿子读大学不愿意读书怎么办 孩子打架全班都讨厌怎么办 孩子不爱学习怎么办 二年级 二年级孩子不爱学习总爱玩怎么办 初三孩子学习不积极怎么办 孩子学习不积极应该怎么办 小孩子贪玩不写作业怎么办 老公懒不帮忙分担家务怎么办 宝宝不喜欢早教课程单一怎么办 孩子上课不听话顶撞老师怎么办 孩子不喜欢吃水果蔬菜怎么办 中学生顶撞家长顶撞老师怎么办