JerryJee

记录生活点滴.

smarty二维数组循环实例

Posted on 十一月 22nd, 2010 by Jerry Jee

array.php文件
<?php
require_once ("../comm/Smarty.class.php");
$smarty = new Smarty();
$smarty->template_dir = "../Tmpl";//设置模板目录
$smarty->compile_dir = ‘../Tmpl_c/’;
$smarty->config_dir = ‘../configs/’;
$smarty->cache_dir = ‘../cache/’;
$smarty->caching = false;
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";

$forum = array(
array("category_id" => 1, "category_name" => "公告","topic" => array(
array("topic_id" => 1, "topic_name" => "站内公告")
)
),
array("category_id" => 2, "category_name" => "文学专区","topic" => array(
array("topic_id" => 2, "topic_name" => "好书介绍"),
array("topic_id" => 3, "topic_name" => "奇闻共赏")
)
),
array("category_id" => 3, "category_name" => "电脑专区","topic" => array(
array("topic_id" => 4, "topic_name" => "硬件周边"),
array("topic_id" => 5, "topic_name" => "论坛")
)
)
);

$smarty->assign("forum", $forum);
$smarty->display("array.tpl");
?>

array.tpl
<{section name=sec1 loop=$forum}>
<{$forum[sec1].category_name}>
<{section name=sec2 loop=$forum[sec1].topic}>
<{$forum[sec1].topic[sec2].topic_name}>
<{/section}>
<{/section}>

Popularity: 1%

Leave a Response

You must be logged in to post a comment.