smarty的基本用法
来源:互联网 发布:免费 流程图制作软件 编辑:程序博客网 时间:2024/05/20 22:28
smarty的基本用法
2009-04-15 20:06
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的数值
教程原创:www.37dg.com(王颂元)
转载请注明出处,谢谢合作
<{ section name=s loop=$my_r }>
<{if $my_r[s]=="456" }> //这是字符型的实例,用==
<{$my_r[s]}>
<{else}>
Not
<{/if}>
<br>
<{ /section }>
首先,使用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的数值
教程原创:www.37dg.com(王颂元)
转载请注明出处,谢谢合作
<{ section name=s loop=$my_r }>
<{if $my_r[s]=="456" }> //这是字符型的实例,用==
<{$my_r[s]}>
<{else}>
Not
<{/if}>
<br>
<{ /section }>
- smarty的基本用法
- smarty的基本使用
- Smarty的基本配置
- Smarty的一些用法
- Smarty的基本配置【smarty.inc.php】
- Smarty 中literal的用法
- Smarty中配置文件的用法
- Smarty模版引擎的一些基本知识点
- Smarty模板引擎基本语法的学习
- PHP 模板引擎Smarty的基本语法
- PHP之Smarty模板的基本使用
- smarty中基本数据的获取方式
- Smarty的基本使用与总结
- smarty模板引擎的基本语法
- smarty中section的嵌套用法
- Smarty 模板定界符的另一种用法
- Smarty变量的用法及效果
- smarty中变量调节器的用法
- HoverAccordion 制作卷帘菜单
- 搭建短信开发平台 基于pxa270开发箱 裁剪烧写内核 等
- 构造函数的理解和程序执行流程
- Abstract class 和 Interface 区别
- C#中的委托和事件
- smarty的基本用法
- 杀狗问题(帽子问题)
- SQL SERVER 数值类型的存储格式及转换
- #error和#progma && 宏定义换行符“\”
- Myeclipse弹出框:Multiple Errors have Occurred
- Hibernate三种状态的区分,以及save,update,saveOrUpdate,merge等的使用
- 关于SQL Server 2005的学习笔记—分析函数
- C#多线程编程(3)-线程池ThreadPool
- asp.net未能加载程序集问题