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初学者头疼问题总结
Jul 08 PHP
用Php实现链结人气统计
Oct 09 PHP
php入门学习知识点二 PHP简单的分页过程与原理
Jul 14 PHP
Thinkphp中volist标签mod控制一定记录的换行BUG解决方法
Nov 04 PHP
smarty中post用法实例
Nov 28 PHP
win7安装php框架Yii的方法
Jan 25 PHP
Yii操作数据库实现动态获取表名的方法
Mar 29 PHP
Smarty模板引擎缓存机制详解
May 23 PHP
php+jQuery递归调用POST循环请求示例
Oct 14 PHP
ThinkPHP框架表单验证操作方法
Jul 19 PHP
PHP中引用类型和值类型功能与用法示例
Feb 26 PHP
PHP-FPM 设置多pool及配置文件重写操作示例
Oct 02 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 cdata 处理(详细介绍)
2013/07/05 PHP
非常实用的php弹出错误警告函数扩展性强
2014/01/17 PHP
php中preg_match的isU代表什么意思
2015/10/01 PHP
LAMP环境使用Composer安装Laravel的方法
2017/03/25 PHP
PHP在弹框中获取foreach中遍历的id值并传递给地址栏
2017/06/13 PHP
php实现微信原生支付(扫码支付)功能
2018/05/30 PHP
PHP抽象类与接口的区别实例详解
2019/05/09 PHP
Javascript this指针
2009/07/30 Javascript
有效的捕获JavaScript焦点的方法小结
2009/10/08 Javascript
网页禁用右键菜单和鼠标拖动选择方法小结
2015/02/25 Javascript
深入理解JavaScript系列(41):设计模式之模板方法详解
2015/03/04 Javascript
javascript精确统计网站访问量实例代码
2015/12/19 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
jQuery实现下拉框左右移动(全部移动,已选移动)
2016/04/15 Javascript
jquery 判断是否支持Placeholder属性的方法
2017/02/07 Javascript
Boostrap栅格系统与自己额外定义的媒体查询的冲突问题
2017/02/19 Javascript
jQuery实现ajax无刷新分页页码控件
2017/02/28 Javascript
js中setTimeout的妙用--防止循环超时
2017/03/06 Javascript
JavaScript运动框架 解决防抖动问题、悬浮对联(二)
2017/05/17 Javascript
JavaScript实现的搜索及高亮显示功能示例
2017/08/14 Javascript
详解vue-cli中的ESlint配置文件eslintrc.js
2017/09/25 Javascript
纯js实现隔行变色效果
2017/11/29 Javascript
微信小程序导航栏跟随滑动效果的实现代码
2019/05/14 Javascript
Python类的定义、继承及类对象使用方法简明教程
2015/05/08 Python
轻量级的Web框架Flask 中模块化应用的实现
2017/09/11 Python
使用Selenium破解新浪微博的四宫格验证码
2018/10/19 Python
django的model操作汇整详解
2019/07/26 Python
简单了解Python变量作用域正确使用方法
2020/06/12 Python
巴西儿童时尚购物网站:Dinda
2019/08/14 全球购物
linux面试题参考答案(11)
2016/11/26 面试题
年度安全生产目标责任书
2014/07/23 职场文书
五心教育心得体会
2014/09/04 职场文书
医院党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
后进生评语大全
2015/01/04 职场文书
Python 流媒体播放器的实现(基于VLC)
2021/04/28 Python
为自由献出你的心脏!「进击的巨人展 FINAL」2022年6月在台开展
2022/04/13 日漫