设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: IT公社 IT community >> 网络编程 >> PHP >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
使用C#开发SmartPhone程…
SQL Artisan多表查询和统…
关于使用文本域(TextAre…
用PHP来验证Email是否正…
使用MaxMind 根据IP地址…
php中检查email完整性
常用email校验语句的解释
一个全面获取图象信息的…
PHP中轻型模板处理类:S…
非常简单的php模版类--D…
  Smarty模板快速入门           
Smarty模板快速入门
 

  〇. 引言       在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}

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2006-11-9
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:admin    责任编辑:admin 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持