smarty实例教程二
时间:2008-05-29 来源:剑心通明
实例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
===============================================
$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日
这个例子是综合使用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
===============================================
$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日
相关阅读 更多 +