免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1610 | 回复: 0
打印 上一主题 下一主题

Smarty教程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-26 16:41 |只看该作者 |倒序浏览

一. 安装      
首先打开网页
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目录下,代码如下:
  1. Smarty
  2. {$hello}
复制代码
           新建index.php,将此文件放在learn/下:
  1. template_dir ="smarty/templates/templates";
  2. $smarty->compile_dir ="smarty/templates/templates_c";
  3. $smarty->config_dir = "smarty/templates/config";
  4. $smarty->cache_dir ="smarty/templates/cache";
  5. //smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
  6. $smarty->caching = false;
  7. $hello = "Hello World!";//赋值
  8. $smarty->assign("hello",$hello);//引用模板文件
  9. $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
notodd", "not", "mod", "div by", "evenby", "odd
by","==","!=",">","="这些是if中可以用到的比较。看看就能知道什么意思吧。
        示例:
        {if $name eq"Fred"}
        WelcomeSir.
        {elseif $name eq"Wilma"}
        WelcomeMa'am.   
        
        Welcome,whatever you are.
        {/if}
五. 循环        
在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。         
示例:
{* this examplewill print out all the values of the $custid array *}
{sectionname=customer loop=$custid}
id: {$custid[customer]}
{/section}
OUTPUT:
id: 1000
id: 1001
id: 1002
六. 常见问题         
Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascript函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。         
示例:
  1.          
  2.          
  3.               function isblank(field) {
  4.                          if (field.value == '')
  5.                                 { return false; }
  6.                          else
  7.                                 {
  8.                                  document.loginform.submit();
  9.                                 return true;
  10.                                 }
  11.               }
  12.          {/literal}
复制代码
如果您认为本词条还有待完善,需要补充新内容或修改错误内容,请留言...
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/84280/showart_2008162.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP