Smarty模板快速入门


Posted in PHP onJanuary 04, 2007

在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/.

    (2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下: 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">   <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 not odd", "not", "mod", "div by", "even by", "odd by","==","!=",">", "<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。

      示例:
      {if $name eq "Fred"}

                     Welcome Sir.

    {elseif $name eq "Wilma"}

                     Welcome Ma'am.   

    {else}
                     Welcome, whatever you are.

    {/if}

五. 循环

       在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。
       示例:
{* this example will print out all the values of the $custid array *}

{section name=customer loop=$custid}

              id: {$custid[customer]}<br>
{/section}

OUTPUT:

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

六. 常见问题

       Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascript函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。
       示例:
{literal} 
       <script language=javascript> 

             function isblank(field) { 

                       if (field.value == '')  
                               { return false; } 

                       else 
                               { 

                               document.loginform.submit(); 
                               return true; 

                               } 

             } 

       </script> 
{/literal} 

PHP 相关文章推荐
PHP 进程锁定问题分析研究
Nov 24 PHP
php 模拟POST提交的2种方法详解
Jun 17 PHP
PHP 验证码不显示只有一个小红叉的解决方法
Sep 30 PHP
学习php分页代码实例
Oct 24 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(五)
Jun 23 PHP
ThinkPHP控制器间实现相互调用的方法
Oct 31 PHP
PHP中is_dir()函数使用指南
May 08 PHP
php使用timthumb生成缩略图的方法
Jan 22 PHP
微信公众号OAuth2.0网页授权问题浅析
Jan 21 PHP
PHP实现的日历功能示例
Sep 01 PHP
strpos() 函数判断字符串中是否包含某字符串的方法
Jan 16 PHP
php 的多进程操作实践案例分析
Feb 28 PHP
菜鸟学PHP之Smarty入门
Jan 04 #PHP
推荐php模板技术[转]
Jan 04 #PHP
推荐个功能齐全的发送PHP邮件类
Jan 03 #PHP
php和js交互一例-PHP教程,PHP应用
Jan 03 #PHP
URL Rewrite的设置方法
Jan 02 #PHP
DISCUZ 分页代码
Jan 02 #PHP
帖几个PHP的无限分类实现想法~
Jan 02 #PHP
You might like
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
2015/12/09 PHP
ThinkPHP中session函数详解
2016/09/14 PHP
PHP对称加密函数实现数据的加密解密
2016/10/27 PHP
JQuery插件fancybox无法在弹出层使用左右键的解决办法
2013/12/25 Javascript
button没写type=button会导致点击时提交
2014/03/06 Javascript
深入探寻seajs的模块化与加载方式
2015/04/14 Javascript
简单实现限制uploadify上传个数
2015/11/16 Javascript
前端框架Vue.js中Directive知识详解
2016/09/12 Javascript
整理一下常见的IE错误
2016/11/18 Javascript
JS实现的相册图片左右滚动完整实例
2016/11/23 Javascript
详解Nodejs基于mongoose模块的增删改查的操作
2016/12/21 NodeJs
微信小程序 常用工具类详解及实例
2017/02/15 Javascript
JS实现的简单四则运算计算器功能示例
2017/09/27 Javascript
详解React之父子组件传递和其它一些要点
2018/06/25 Javascript
详解基于React.js和Node.js的SSR实现方案
2019/03/21 Javascript
原生js实现弹窗消息动画
2020/11/20 Javascript
[03:03]DOTA2校园争霸赛 济南城市决赛欢乐发奖活动
2013/10/21 DOTA
学习python处理python编码问题
2011/03/13 Python
教你如何在Django 1.6中正确使用 Signal
2014/06/22 Python
python中readline判断文件读取结束的方法
2014/11/08 Python
python实现telnet客户端的方法
2015/04/15 Python
python使用threading获取线程函数返回值的实现方法
2017/11/15 Python
python梯度下降法的简单示例
2018/08/31 Python
python Pandas如何对数据集随机抽样
2019/07/29 Python
python-tornado的接口用swagger进行包装的实例
2019/08/29 Python
Python callable内置函数原理解析
2020/03/05 Python
django xadmin action兼容自定义model权限教程
2020/03/30 Python
Python openpyxl模块实现excel读写操作
2020/06/30 Python
投标邀请书范文
2014/01/31 职场文书
会计专业自我鉴定
2014/02/10 职场文书
六查六看自检自查剖析材料
2014/10/14 职场文书
高三毕业评语
2014/12/31 职场文书
先进党支部事迹材料2016
2016/02/26 职场文书
2019最新公司租房合同(例文)
2019/07/18 职场文书
用python基于appium模块开发一个自动收取能量的小助手
2021/09/25 Python
Go 内联优化让程序员爱不释手
2022/06/21 Golang