`

smarty整理总结

    博客分类:
  • PHP
阅读更多

1.smarty的配置
      首先,使用smarty第一件事是先配置好,一般有以下9行代码

require_once("smarty/libs/Smarty_class.php"); //把smarty的类定义文件包含进来
      $smarty=new smarty();
      $smarty->config_dir="smarty/libs/Config_File.class.php";
      $smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
      $smarty->cache_dir="smarty_cache/"; //缓存文件夹
      $smarty->template_dir="smarty_tpl";           //模板文件夹
      $smarty->compile_dir="smarty_compile";      //编译文件夹
      $smarty->left_delimiter="<{";            // 标签符定义不是必要的,smarty默认是使用"<"和">",强烈建议更换。
                                         //因为如果smarty的标签刚好在javascript语句里面时,冲突的可能性很大
      $smarty->right_delimiter="}>";

      以上的9行代码可以放在一个独立的文件,需要使用smarty的页面引用进来即可


2. smarty的使用
      smarty替换标签的语法:
      smarty->assign("标签名","值");
      smarty->display("index.html"); //显示内容,index为模板文件名

      假定模板文件里面有一个标签 <{ $user_name }> (注意:标签里面的变量必须带$)
那么在PHP文件可以:
      $new_name="Joan";
      smarty->assign("user_name",$new_name); (注意:在此时,user_name是不带$的)
      smarty->display("index.html"); //显示内容,index为模板文件名


3. smarty的循环
      循环的使用在php文件像处理普通标签一样处理
     
      模板文件代码:
      <table border=1 width=500>
<{section name=s loop=$stu}>
      <tr>
      <td>
           <{$stu[s]}>
      </td>
      </tr>
<{/section}>
      </table>

那么在PHP文件可以:
假如有一个数组,或一个记录集 $rs
$smarty->assign("stu",$rs);
$smarty->display("index.html"); //在PHP文件处理循环标签和普通标签没什么区别。
教程原创:www.37dg.com(王颂元)
转载请注明出处,谢谢合作

4.      smarty的if 语法
      模板文件
      <{if $my_r>500 }>
           <{$my_r}>
      <{/if}>

      php文件:

      $aa=123;
      $smarty->assign("my_r",$aa);     
      $smarty->display("in.html");
上例中,如果$aa>500,my_r的标签就能显示出来,否则不显示。
<{ if 条件 }> //此处的条件不要加括号,也不要像basic语言那样在后面加 then

<{/if}>

5. smarty循环配合if 使用实例

      PHP文件
----------------------------------
      $aa[0]=123;
      $aa[1]=456;
      $aa[2]=789;
      $smarty->assign("my_r",$aa);
      $smarty->display("in.html");

      模板文件
------------------------------------------------------
      <{ section name=s loop=$my_r }>
      <{if $my_r[s]>200 }>
      <{$my_r[s]}>
      <{else}>
           小于200
      <{/if}>
      <br>
      <{/section}>
-----------------------------------------------
上例中,只显示大于200的数值

 

本文来源于:

http://z04ds334.iteye.com/blog/324512

分享到:
评论

相关推荐

    smarty的总结

    smarty的总结 学习有易于 学好smarty很有帮助 不信你可以看看哦

    Smarty

    Smarty Smarty Smarty

    smarty安装与入门

    smarty安装及初级使用 在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。现在就用30分钟让您...

    smarty3.0,smarty最新版本

    smarty3.0,smarty最新版本

    Smarty_smarty_

    Smarty 模板引擎教程,内容主要有基本语法、变量、组合修改器、内建函数、自定义函数、配置文件、控制台调试、缓存、插件扩展、使用技巧和经验等,比较详细的一个中文手册

    Smarty最新版下载自Smarty官网

    Smarty最新版官网,Smarty最新版下载自Smarty官网

    smarty3 的应用详解

    $smarty= Smarty::instance(); 模板 之前的smarty模板,相当于重新定义了一套标签语言,那么smarty3提供了一种新的模板形式,直接支持php语法的模板。 但是问题就出来了,我们还有必要用模板吗? 引用php类型模板...

    php模版 (smarty)php模版 (smarty)php模版 (smarty)

    php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)php模版 (smarty)

    smarty运算符

    php5会使用的smarty运算符,smarty是在php前期使用中常见的模版分离,同时国内鼎鼎大名的ecshop也是采用这样方式开发系统

    自己写的smarty小框架

    smarty 迷你

    smarty-3.1.29

    2015年12月24日最新版:smarty-3.1.29

    smarty模板中文手册

    1. 速度:采用Smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。 2. 编译型:采用Smarty编写的程序在运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的...

    smarty中section的使用

    smarty中section的使用 smarty section php

    5分钟搞定smarty

    5分钟搞定smarty5分钟搞定smarty

    php之Smarty模板引擎

    php Smarty

    smarty模板,调用静态页面模板,Smarty-2.6.22

    smarty模板,调用静态页面模板,Smarty-2.6.22

    smarty3 入门实例

    smarty3 入门实例,Smarty-3.1.16。

    smarty3.6 带例子

    smarty smarty3.6 带例子 下载,好多例子啊

    smarty2.6.26

    smarty2.6.26模板 smarty2.6.26模板 smarty2.6.26模板 smarty2.6.26模板 php

    smarty手册.chm

    {$smarty} reserved variable [{$smarty}保留变量] 5. Variable Modifiers [变量调节器] capitalize [首字符大写] count_characters [字符计数] cat [连接字符串] count_paragraphs [计算段数] count_...

Global site tag (gtag.js) - Google Analytics