Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17912483
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-05-29 22:08:56

实例2:
    这个例子是综合使用smarty模板参数的一个例子,这些参数用来控制模板的输出,我只选其中几个,其它的参数你去看参考吧。

以下为引用的内容:
  ================================================
      example2.tpl
    ================================================
   
      大师兄smarty示例2
     
        1. 第一句首字母要大写:{$str1|capitalize}

        2. 第二句模板变量 + 李晓军:{$str2|cat:"李晓军"}

        3. 第三句输出当前日期:{$str3|date_format:"%Y年%m月%d日"}

        4. 第四句.php程序中不处理,它显示默认值:{$str4|default:"没有值!"}

        5. 第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:

         {$str5|indent:8:"*"}}

        6. 第六句把全部变为小写:{$str6|lower}

        7. 第七句把变量中的teacherli替换成:李晓军:{$str7|replace:"teacherli":"李晓军"}

    8. 第八句为组合使用变量修改器:{$str8|capitalize|cat:"这里是新加的时间:"|date_format:"%Y年%m月%d日"|lower}
     
   

    ===============================================
    example2 .php
    ===============================================
         /*********************************************
      *
      * 文件名: example2.php
      * 作  用: 显示实例程序2
      *
      * 作  者: 大师兄
      * Email: 
      *
      *********************************************/
      include_once("./comm/Smarty.class.php"); //包含smarty类文件

      $smarty = new Smarty();  //建立smarty实例对象$smarty
      $smarty->template_dir = "./templates";//设置模板目录
      $smarty->compile_dir  = "./templates_c"; //设置编译目录
     
      //----------------------------------------------------
      //左右边界符,默认为{},但实际应用当中容易与JavaScript
      //相冲突,所以建议设成<{}>或其它。
      //----------------------------------------------------
      $smarty->left_delimiter = "{";
      $smarty->right_delimiter = "}";

      $smarty->assign("str1", "my name is xiao jun, li."); //将str1替换成My Name Is Xiao Jun, Li.
      $smarty->assign("str2", "我的名字叫:"); //输出: 我的名字叫:李晓军
      $smarty->assign("str3", "公元"); //输出公元2004年8月21日(我的当前时间)
      //$smarty->assign("str4", ""); //第四句不处理时会显示默认值,如果使用前面这一句则替换为""
      $smarty->assign("str5", "前边8个*"); //第五句输出:********前边8个*
      $smarty->assign("str6", ""); //这里将输出
      $smarty->assign("str7", "this is teacherli"); //在模板中显示为:this is 李晓军
      $smarty->assign("str8", "HERE IS COMBINING:");

      //编译并显示位于./templates下的index.tpl模板
      $smarty->display("example2.tpl");
   ?>

最终输出效果:
======================================================
example2.php输出效果:
======================================================

大师兄smarty示例2

1. 第一句首字母要大写:My Name Is Xiao Jun, Li.

2. 第二句模板变量 + 李晓军:我的名字叫:李晓军

3. 第三句输出当前日期:公元2004年8月21日

4. 第四句.php程序中不处理,它显示默认值:没有值!

5。第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:

********前边8个*

6. 第六句把TEACHerLI@163.com全部变为小写:teacherli@163.com

7. 第七句把变量中的teacherli替换成:李晓军:this is 李晓军

8. 第八句为组合使用变量修改器:Here is Combining:这里是新加的时间:2004年8月21日


阅读(544) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~