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 相关文章推荐
Apache, PHP在Windows 9x/NT下的安装与配置 (一)
Oct 09 PHP
Windows下的PHP5.0详解
Nov 18 PHP
echo(),print(),print_r()之间的区别?
Nov 19 PHP
用php获取远程图片并把它保存到本地的代码
Apr 07 PHP
PHP 文章中的远程图片采集到本地的代码
Jul 30 PHP
浅谈PHP强制类型转换,慎用!
Jun 06 PHP
ThinkPHP3.1新特性之对分组支持的改进与完善概述
Jun 19 PHP
PHP函数in_array()使用详解
Aug 20 PHP
php使用pdo连接报错Connection failed SQLSTATE的解决方法
Dec 15 PHP
PHP调用Linux命令权限不足问题解决方法
Feb 07 PHP
yii2.0实现验证用户名与邮箱功能
Dec 22 PHP
PHP实现时间日期友好显示实现代码
Sep 08 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 冒泡排序算法的实现代码
2010/08/08 PHP
php设计模式 Adapter(适配器模式)
2011/06/26 PHP
php实现在新浪云中使用imagick生成缩略图并上传的方法
2016/09/26 PHP
php实现查询功能(数据访问)
2017/05/23 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
一些javascript一些题目的解析
2010/12/25 Javascript
Javascript模块化编程(一)模块的写法最佳实践
2013/01/17 Javascript
JavaScript制作windows经典扫雷小游戏
2015/03/31 Javascript
基于JavaScript实现本地图片预览
2017/02/08 Javascript
vue如何从接口请求数据
2017/06/22 Javascript
深入浅出webpack教程系列_安装与基本打包用法和命令参数详解
2017/09/10 Javascript
微信小程序返回多级页面的实现方法
2017/10/27 Javascript
js使用cookie实现记住用户名功能示例
2019/06/13 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
Python基于pygame实现图片代替鼠标移动效果
2015/11/11 Python
python直接访问私有属性的简单方法
2016/07/25 Python
numpy中以文本的方式存储以及读取数据方法
2018/06/04 Python
对pandas通过索引提取dataframe的行方法详解
2019/02/01 Python
Python3实现计算两个数组的交集算法示例
2019/04/03 Python
VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法详解
2019/07/01 Python
Python日志无延迟实时写入的示例
2019/07/11 Python
python web框架Flask实现图形验证码及验证码的动态刷新实例
2019/10/14 Python
Python 3 使用Pillow生成漂亮的分形树图片
2019/12/24 Python
Android Q之气泡弹窗的实现示例
2020/06/23 Python
pycharm 如何取消连按两下shift出现的全局搜索
2021/01/15 Python
CSS3中颜色线性渐变实战
2015/07/18 HTML / CSS
HTML5开发动态音频图的实现
2020/07/02 HTML / CSS
德国足球商店:OUTFITTER
2019/05/06 全球购物
英国羊皮鞋类领先品牌:Just Sheepskin
2019/12/12 全球购物
linux面试题参考答案(7)
2012/10/29 面试题
UNIX操作系统结构由哪几部分组成
2016/02/17 面试题
经济管理专业自荐信
2013/12/30 职场文书
体育比赛口号
2014/06/09 职场文书
机械电子工程专业自荐书
2014/06/10 职场文书
总经理助理岗位职责范本
2014/07/20 职场文书
海上钢琴师的观后感
2015/06/11 职场文书