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 相关文章推荐
MVC模式的PHP实现
Oct 09 PHP
用PHP实现将GB编码转换为UTF8
Nov 25 PHP
php基础知识:控制结构
Dec 13 PHP
php mysql数据库操作分页类
Jun 04 PHP
php 全文搜索和替换的实现代码
Jul 29 PHP
php 获取select下拉列表框的值
May 08 PHP
通过JavaScript或PHP检测Android设备的代码
Mar 09 PHP
div li的多行多列 无刷新分页示例代码
Oct 16 PHP
php模板原理讲解
Nov 13 PHP
PHP实现的购物车类实例
Jun 17 PHP
php+mysql+ajax 局部刷新点赞/取消点赞功能(每个账号只点赞一次)
Jul 24 PHP
解决Laravel使用验证时跳转到首页的问题
Nov 17 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/11/16 PHP
php实现cc攻击防御和防止快速刷新页面示例
2014/02/13 PHP
php实现aes加密类分享
2014/02/16 PHP
PHP中ini_set和ini_get函数的用法小结
2014/02/18 PHP
PHP使用mysql与mysqli连接Mysql数据库用法示例
2016/07/07 PHP
Display SQL Server Login Mode
2007/06/21 Javascript
Javascript this指针
2009/07/30 Javascript
JQuery 遮罩层实现(mask)实现代码
2010/01/09 Javascript
jQuery 前的按键判断代码
2010/03/19 Javascript
JavaScript call apply使用 JavaScript对象的方法绑定到DOM事件后this指向问题
2011/09/28 Javascript
js实现支持手机滑动切换的轮播图片效果实例
2015/04/29 Javascript
比例尺、缩略图、平移缩放之百度地图添加控件方法
2015/08/03 Javascript
jQuery Validate表单验证插件 添加class属性形式的校验
2016/01/18 Javascript
总结js函数相关知识点
2018/02/27 Javascript
详解vue2.0+axios+mock+axios-mock+adapter实现登陆
2018/07/19 Javascript
微信小程序人脸识别功能代码实例
2019/05/07 Javascript
python获取糗百图片代码实例
2013/12/18 Python
12步入门Python中的decorator装饰器使用方法
2016/06/20 Python
python用Pygal如何生成漂亮的SVG图像详解
2017/02/10 Python
python+opencv实现的简单人脸识别代码示例
2017/11/14 Python
Python使用cx_Oracle模块操作Oracle数据库详解
2018/05/07 Python
Python 爬虫之Beautiful Soup模块使用指南
2018/07/05 Python
Python3随机漫步生成数据并绘制
2018/08/27 Python
pycharm恢复默认设置或者是替换pycharm的解释器实例
2018/10/29 Python
python实现nao机器人手臂动作控制
2019/04/29 Python
Django实现基于类的分页功能
2019/10/31 Python
python base64库给用户名或密码加密的流程
2020/01/02 Python
Django如何重置migration的几种情景
2021/02/24 Python
HTML5 实现一个访问本地文件的实例
2012/12/13 HTML / CSS
html特殊符号示例 html特殊字符编码对照表
2014/01/14 HTML / CSS
史蒂夫·马登加拿大官网:Steve Madden加拿大
2017/11/18 全球购物
CK加拿大官网:Calvin Klein加拿大
2020/03/14 全球购物
企业员工薪酬方案
2014/06/04 职场文书
社区爱国卫生月活动总结
2014/06/30 职场文书
八一建军节演讲稿
2014/09/10 职场文书
MySql分区类型及创建分区的方法
2022/04/13 MySQL