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 相关文章推荐
超强分页类2.0发布,支持自定义风格,默认4种显示模式
Jan 02 PHP
使用bcompiler对PHP文件进行加密的代码
Aug 29 PHP
PHP Undefined index报错的修复方法
Jul 17 PHP
php页码形式分页函数支持静态化地址及ajax分页
Mar 28 PHP
smarty模板中使用get、post、request、cookies、session变量的方法
Apr 24 PHP
PHP利用APC模块实现文件上传进度条的方法
Jan 26 PHP
利用PHP命令行模式采集股票趋势信息
Aug 09 PHP
thinkphp制作404跳转页的简单实现方法
Sep 22 PHP
php基于dom实现的图书xml格式数据示例
Feb 03 PHP
php可变长参数处理函数详解
Feb 22 PHP
PHP自动补全表单的两种方法
Mar 06 PHP
PHP array_reduce()函数的应用解析
Oct 28 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
很温暖很温暖的Lester Young
2021/03/03 冲泡冲煮
模仿OSO的论坛(二)
2006/10/09 PHP
php源代码安装常见错误与解决办法分享
2013/05/28 PHP
ThinkPHP中URL路径访问与模块控制器之间的关系
2014/08/23 PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
2016/11/01 PHP
PHP房贷计算器实例代码,等额本息,等额本金
2017/04/01 PHP
为jquery.ui.dialog 增加“自动记住关闭时的位置”的功能
2009/11/24 Javascript
javascript实现3D变换的立体圆圈实例
2015/08/06 Javascript
javascript中substring()、substr()、slice()的区别
2015/08/30 Javascript
jQuery实现图片加载完成后改变图片大小的方法
2016/03/29 Javascript
web前端vue filter 过滤器
2018/01/12 Javascript
利用Vue实现一个markdown编辑器实例代码
2019/05/19 Javascript
js实现简单的日历显示效果函数示例
2019/11/25 Javascript
JS浏览器BOM常见操作实例详解
2020/04/27 Javascript
再也不怕 JavaScript 报错了,怎么看怎么处理都在这儿
2020/12/09 Javascript
[47:21]Liquid vs TNC Supermajor 胜者组 BO3 第一场 6.4
2018/06/05 DOTA
python操作ssh实现服务器日志下载的方法
2015/06/03 Python
常见python正则用法的简单实例
2016/06/21 Python
python logging模块书写日志以及日志分割详解
2019/07/22 Python
Python中IP地址处理IPy模块的方法
2019/08/16 Python
python+mysql实现个人论文管理系统
2019/10/25 Python
关于sys.stdout和print的区别详解
2019/12/05 Python
opencv python Canny边缘提取实现过程解析
2020/02/03 Python
Python爬虫实例——爬取美团美食数据
2020/07/15 Python
css3实现背景图片拉伸效果像桌面壁纸一样
2013/08/19 HTML / CSS
HTML5拖拽文件到浏览器并实现文件上传下载功能代码
2013/06/06 HTML / CSS
生物化学研究助理员求职信
2013/10/09 职场文书
个人自荐信
2013/12/05 职场文书
英文自我鉴定
2013/12/10 职场文书
写求职信有哪些注意事项
2014/05/08 职场文书
小学向国旗敬礼活动方案
2014/09/27 职场文书
党的群众路线教育实践活动领导班子整改方案
2014/10/25 职场文书
2014大学班主任工作总结
2014/11/08 职场文书
2015暑期社会实践通讯稿
2015/07/18 职场文书
建立共青团委员会的请示
2019/04/02 职场文书
成本低的5个创业项目:投资小、赚钱快
2019/08/20 职场文书