smarty:解决php和html代码分离。
1.1 smarty安装
1>http://smarty.php.net 下载
2>解压后 libs文件夹,smarty核心文件夹。拷贝到你的工作目录。libs/Smarty.class.php
3>和libs平级的目录下,创建文件夹
templates 网页模板:*.html ***********
templates_c 编译后的代码
configs 配置文件
cache 缓存所有的模板
1.2 smarty的配置
1、案例
smarty_02.php
require_once(“libs/Smarty.class.php”); //引入
$smarty= new Smarty(); //实例化一个对象。
$smarty->assign(“awt”,$a); //为模板当中的变量赋值。同时可为数组赋值
$smarty->display(“smarty_02.htm”); //模板页调用
smarty_02.htm
可以调用数组。?????????
2、smarty配置。
/* 改变smarty的配置目录结构。
$smarty->templates = “a/templates”;
$smarty->templates_c = “a/templates_c”;
$smarty->configs = “a/configs”;
$smarty->cache = “a/cache”;
*/
3、模板页当中,如果写样式,因为有{},引起语义混乱,出错。
1》新建css文件。把样式写在css文件中,避免了语义混乱了。
在模板中如果调用css样式,是以php文件为主。
2》可以改变模板中的定界符“{}”==》“<!–{}–>”
$smarty->left_delimiter = “<!–{“;
$smarty->right_delimiter = “}–>”;
smarty语法
1、控制结构
<!–{if 条件1}–>
条件1成立的代码
<!–{elseif 条件2}–>
条件1成立的代码
<!–{elseif 条件3}–>
条件3成立的代码
……….
<!–{else}–>
以上条件都不成立执行else
<!–{/if}–>
2、循环结构
while for do..while foreach
1》foreach
格式:
{foreach from=数组名称 item=数组当前元素名}
循环体。
{/foreach}
例://————————-测试foreach———————–
2》section
格式:
{section name=”当前元素” loop=”数组名称”}
循环体
{/section}
例://————————-测试section———————–