一聚教程网:一个值得你收藏的教程网站

热门教程

smarty入门教程二

时间:2022-07-02 10:49:33 编辑:袖梨 来源:一聚教程网


实例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. 第六句把TEACHerLI@163.com全部变为小写:{$str6|lower}

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

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


example2 .php

PHP:
/*********************************************
*
* 文件名: example2.php
* 作 用: 显示实例程序2
*
* 作 者: 大师兄
* Email: teacherli@163.com
* 修 正: forest
*********************************************/
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", "TEACHerLI@163.com"); //这里将输出teacherli@163.com
$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日


在模板中的这些参数被称为变量修改器(variable modifiers),使用这些参数可对模板进行一系列的修改控制。变量修改器
使用"|"和调节器名称应用修改器, 使用":"分开修改器参数。变量修改器可以组合使用,像第八句一样,实际使用中可以灵活应用。

热门栏目