smarty数组遍历
一、foreach和section遍历数组的内容
foreach:
语法:<{foreach name from item key }>语句<{foreachelse}>语句2<{/foreach}>
key:当前元素的键值 name:当前循环的名字
form:循环数组的名称 item:当前处理元素的变量名称
section:
语法:<{section}> <{/section}>
name :该循环的名称(自定义)
loop:决定循环次数的变量名称(数组名)
section不能遍历关联数组
if语句:
<{if 条件}> 语句1<{elseif 条件2}>语句2<{else}>语句3<{/if}>
二、把数组以表格的形式输出
1、
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();//创建smarty对象
$smarty->template_dir="demo/templates";//修改模板存放路径
$smarty->compile_dir="demo/tempaltes_c";//修改编译路径
$smarty->left_delimiter="<{";//修改定界符
$smarty->right_delimiter="}>";
$arr=array(
array("id"=>"1","name"=>"张三","age"=>18),
array("id"=>"2","name"=>"李四","age"=>"19")
);
$ar=array(
array("1","张三",18),
array("2","李四","19")
);
$smarty->assign("arr",$arr);//给模板中的变量赋值
$smarty->assign("ar",$ar);
$smarty->display("test3.tpl");
?>
2、test3.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table width="200" border="1">
<caption>一个一个的写值</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<td><{$arr[0].id}></td>
<td><{$arr[0].name}></td>
<td><{$arr[0].age}></td>
</tr>
<tr>
<td><{$arr[1].id}></td>
<td><{$arr[1].name}></td>
<td><{$arr[1].age}></td>
</tr>
</table>
<br>
<table width="200" border="1">
<caption>foreach遍历一条</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<{foreach from=$arr[0] item=value}>
<td><{$value}></td>
<{/foreach}>
</tr>
</table>
<br>
<table width="200" border="1">
<caption>foreach遍历全部</caption>
<th>id</th><th>name</th><th>age</th>
<{foreach from=$arr item=row }><!--row=$row[0],$row[1]..,获得外层所有的数组,都遍历出来-->
<tr>
<{foreach from=$row item=value}><!--$row是一个数组,在获得里面的值,value=1,张三,18-->
<td><{$value}></td>
<{/foreach}>
</tr>
<{/foreach}>
</table>
<br>
<table width="200" border="1">
<caption>section遍历全部</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<{section name=loop_a loop=$arr}><{*loop决定循环次数的变量名称,已经决定次数*}>
<td><{$arr[loop_a].id}></td>
<td><{$arr[loop_a].name}></td>
<td><{$arr[loop_a].age}></td></tr>
<{/section}>
</table>
<br>
<table width="200" border="1">
<caption>section遍历一条</caption>
<th>id</th><th>name</th><th>age</th>
<tr>
<{section name=loop_a loop=$ar[1]}>
<td><{$ar[1][loop_a]}></td><{*section不能遍历关联数组*}>
<{/section}>
</tr>
</table>
<br>
<table width="200" border="1">
<caption>section遍历索引全部</caption>
<th>id</th><th>name</th><th>age</th>
<{section name=row loop=$ar}>
<tr>
<{section name=col loop=[row]}>
<td><{$ar[row][col]}></td>
<{/section}>
</tr>
<{/section}>
</table>
</body>
</html>
三、if elseif else,如果选择左边把你输入的内容显示在左边,如果选择是右把你输入的内容显示在右边。。。
1、if.html
<body>
<form action="if.php" method="post">
<input type="text" name="text" value="aaa">
<input type="radio" value="lift"name="radio">lift
<input type="radio" value="right" name="radio">right
<input type="radio" value="center" name="radio">center
<input type="submit" value="提交">
</form>
2、if.php
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/tempaltes_c";
//$smarty->lift_delimiter="<{";
//$smarty->right_delimiter="";
$smarty->assign("test",$_POST["text"]);
$smarty->assign("radio",$_POST["radio"]);
$smarty->display("if.tpl");
?>
3、if.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
{if $radio=='lift'}
<span style="float:left;"> {$test} </span>
{elseif $radio=='right'}
<span style="float:right;">{$test}</span>
{else}
<center>{$test} </center>
{/if}
</body>
</html>