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 daodb插入、更新与删除数据
Mar 19 PHP
php str_pad() 将字符串填充成指定长度的字符串
Feb 23 PHP
windows下升级PHP到5.3.3的过程及注意事项
Oct 12 PHP
php实现无限级分类实现代码(递归方法)
Jan 01 PHP
谨慎使用PHP的引用原因分析
Sep 06 PHP
带密匙的php加密解密示例分享
Jan 29 PHP
浅析ThinkPHP的模板输出功能
Jul 01 PHP
php封装的数据库函数与用法示例【参考thinkPHP】
Nov 08 PHP
PHP解决中文乱码
Apr 28 PHP
基于php编程规范(详解)
Aug 17 PHP
PHP微信H5支付开发实例
Jul 25 PHP
yii2.0框架使用 beforeAction 防非法登陆的方法分析
Sep 11 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遍历数组的方法汇总
2015/04/30 PHP
通过PHP设置BugFree获取邮箱通知
2019/04/25 PHP
Yii2框架自定义类统一处理url操作示例
2019/05/25 PHP
Laravel模糊查询区分大小写的实例
2019/09/29 PHP
php下的原生ajax请求用法实例分析
2020/02/28 PHP
利用ASP发送和接收XML数据的处理方法与代码
2007/11/13 Javascript
JavaScript 事件记录使用说明
2009/10/20 Javascript
jquery 双色表格实现代码
2009/12/08 Javascript
JavaScript中的类继承
2010/11/25 Javascript
JavaScript SetInterval与setTimeout使用方法详解
2013/11/15 Javascript
js和jquery中循环的退出和继续下一个循环
2014/09/03 Javascript
AngularJS内置指令
2015/02/04 Javascript
JavaScript中函数表达式和函数声明及函数声明与函数表达式的不同
2015/11/15 Javascript
前端设计师们最常用的JS代码汇总
2016/09/25 Javascript
JS判断来路是否是百度等搜索索引进行弹窗或自动跳转的实现代码
2016/10/09 Javascript
Vue中使用clipboard实现复制功能
2018/09/05 Javascript
解决vuejs 使用value in list 循环遍历数组出现警告的问题
2018/09/26 Javascript
在Create React App中使用CSS Modules的方法示例
2019/01/15 Javascript
python中MySQLdb模块用法实例
2014/11/10 Python
Python3.7 dataclass使用指南小结
2019/02/22 Python
Python使用requests xpath 并开启多线程爬取西刺代理ip实例
2020/03/06 Python
Python unittest如何生成HTMLTestRunner模块
2020/09/08 Python
python元组拆包实现方法
2021/02/28 Python
英国男士时尚购物网站:Stuarts London
2017/10/22 全球购物
美国在线打印网站:Overnight Prints
2018/10/11 全球购物
服装促销活动方案
2014/02/23 职场文书
院领导写的就业推荐信
2014/03/09 职场文书
村干部承诺书
2014/03/28 职场文书
个人培训自我鉴定
2014/03/28 职场文书
学校安全责任书
2014/04/14 职场文书
出国留学计划书
2014/04/27 职场文书
文明工地标语
2014/06/16 职场文书
2014年个人思想工作总结
2014/11/27 职场文书
经典祝酒词大全
2015/08/12 职场文书
幼儿教师继续教育培训心得体会
2016/01/19 职场文书
MySQL中连接查询和子查询的问题
2021/09/04 MySQL