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 相关文章推荐
PHPMyAdmin 快速配置方法
May 11 PHP
关于Appserv无法打开localhost问题的解决方法
Oct 16 PHP
php中去除所有js,html,css代码
Oct 12 PHP
PHP中nowdoc和heredoc使用需要注意的一点
Mar 21 PHP
PHP二维数组排序的3种方法和自定义函数分享
Apr 09 PHP
php获取中文拼音首字母类和函数分享
Apr 24 PHP
php准确计算复活节日期的方法
Apr 18 PHP
Yii快速入门经典教程
Dec 28 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
Feb 07 PHP
Laravel如何使用Redis共享Session
Feb 23 PHP
laravel中的一些简单实用功能
Nov 03 PHP
PHP通过文件保存和更新信息的方法分析
Sep 12 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
用文本文件实现的动态实时发布新闻的程序
2006/10/09 PHP
浅析PHP中的UNICODE 编码与解码
2013/06/29 PHP
destoon实现会员商铺中指定会员或会员组投放广告的方法
2014/08/21 PHP
php实现异步将远程链接上内容(图片或内容)写到本地的方法
2016/11/30 PHP
php多进程中的阻塞与非阻塞操作实例分析
2020/03/04 PHP
PHP基于openssl实现非对称加密代码实例
2020/06/19 PHP
JS location几个方法小姐
2008/07/09 Javascript
asp.net下利用js实现返回上一页的实现方法小集
2009/11/24 Javascript
js 输出内容到新窗口具体实现代码
2013/05/31 Javascript
Javascript闭包(Closure)详解
2015/05/05 Javascript
详解AngularJS中的依赖注入机制
2015/06/17 Javascript
学习jQuey中的return false
2015/12/18 Javascript
Bootstrap的Refresh Icon也spin起来
2016/07/13 Javascript
JavaScript中捕获/阻止捕获、冒泡/阻止冒泡方法
2016/12/07 Javascript
nodejs取得当前执行路径的方法
2018/05/13 NodeJs
js中的闭包实例展示
2018/11/01 Javascript
Javascript读取上传文件内容/类型/字节数
2019/04/30 Javascript
VUE单页面切换动画代码(全网最好的切换效果)
2019/10/31 Javascript
node.js实现简单的压缩/解压缩功能示例
2019/11/05 Javascript
微信小程序调用wx.getImageInfo遇到的坑解决
2020/05/31 Javascript
解决vue中axios设置超时(超过5分钟)没反应的问题
2020/09/04 Javascript
Python中list初始化方法示例
2016/09/18 Python
numpy基础教程之np.linalg
2019/02/12 Python
对python文件读写的缓冲行为详解
2019/02/13 Python
python实现扫描局域网指定网段ip的方法
2019/04/16 Python
pycharm配置当鼠标悬停时快速提示方法参数
2019/07/31 Python
Django admin禁用编辑链接和添加删除操作详解
2019/11/15 Python
Python中猜拳游戏与猜筛子游戏的实现方法
2020/09/04 Python
python中字典增加和删除使用方法
2020/09/30 Python
CSS3实现瀑布流布局与无限加载图片相册的实例代码
2016/12/22 HTML / CSS
泰国在线书店:SE-ED
2020/06/21 全球购物
C#实现对任一张表的数据进行增,删,改,查要求,运用Webservice,体现出三层架构
2014/07/11 面试题
走进敬老院活动总结
2014/07/10 职场文书
如何正确理解python装饰器
2021/06/15 Python
nginx容器方式反向代理实战
2022/04/18 Servers
HTML页面中使两个div并排显示的实现
2022/05/15 HTML / CSS