<?php
include_once("libs/Smarty.class.php"); // 包含smarty类文件
$smarty=new Smarty(); // 建立smarty实例对象$smarty
/*
$smarty->caching=false; // 是否使用缓存
$smarty->template_dir="./templates/"; // 设置模版目录
$smarty->compile_dir="./templates_c/"; //设置编译目录
$smarty->cache_dir="./smarty_cache/"; // 缓存文件夹
*/
// 设置作用边界符 默认是{ } 一般用<{ }>
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->assign("aa","hello");
$smarty->assign("bb",567);
$smarty->assign("cc",56.7);
$smarty->assign("dd",true);
// 1.一维数组的索引数组
$arr1 = array('北京','上海','天津');
//2.一维数组的关联数组
$arr2 = array('city1'=>'北京','city2'=>'上海','city3'=>'天津');
//3.二维数组的索引数组
$arr3 = array(array('北京','上海','天津'),array('1111','2222','3333'));
//4.二维数组的关联数组
$arr4 = array(array('city1'=>'北京','city2'=>'上海','city3'=>'天津'),array('name'=>'xiao','age'=>60,'city'=>'天津'));
$arr5 = array('emp1'=>array('name1'=>'xiao1','age1'=>601,'city1'=>'天津1'),'emp2'=>array('name2'=>'xiao2','age2'=>602,'city2'=>'天津2'));
$smarty->assign("arr1",$arr1);
$smarty->assign("arr2",$arr2);
$smarty->assign("arr3",$arr3);
$smarty->assign("arr4",$arr4);
$smarty->assign("arr5",$arr5);
$smarty->display("hello.tpl");
?>
<{config_load file='../config/my.conf'}>
<!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>
<{* from是需要循环的数组名字 item是当前元素的变量名 key当前关键字的变量名 *}>
<{foreach from=$arr2 item=temp key=k}>
<{$k}>=<{$temp}>
<{/foreach}>
<h1>循环取出二维索引数组</h1>
<{foreach from=$arr3 item=temp}>
<{foreach from=$temp item=val key=k}>
<{$val}>
<{/foreach}>
<br />
<{/foreach}>
<h1>取出二维的关联数组</h1>
<{foreach from=$arr4 item=temp}>
<{* 全部取出这里需要再次循环 *}>
<!--<{foreach from=$temp item=val key=k}>
<{$k}>=<{$val}>
<{/foreach}>-->
<{取出具体的某一项这里只需要一次循环}>
<{$temp.city}>
<{$temp.name}>
<br />
<{/foreach}>
</body>
</html>