<?php $total = 21000; $tpl->assign("total", $total); $tpl->assign("format_total", number_format($total)); ?>
而 Smarty 的模版就可以这样写: (number_format 修饰函式请到Smarty 官方网页下载) <input name="total" type="hidden" value="<{$total}>" /> 总金额:<{$total|number_format:""}> 元 Smarty 的 PHP 程序中只要这样写:
<?php $total = 21000; $tpl->assign("total", $total); ?>
所以在 Smarty 中我们只要指定一次变量,剩下的交给模版自行决定即可。这样了解了吗?这就是让模版自行决定变量呈现风貌的好处! 控制模版的内容 重复的区块 在 Smarty 样板中,我们要重复一个区块有两种方式: foreach 及 section 。而在程序中我们则要 assign 一个数组,这个数组中可以包含数组数组。就像下面这个例子: 首先我们来看 PHP 程序是如何写的: test2.php:
<?php require "main.php"; $array1 = array(1 => "苹果", 2 => "菠萝", 3 => "香蕉", 4 => "芭乐"); $tpl->assign("array1", $array1); $array2 = array( array("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3"), array("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3"), array("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3"), array("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3"), array("index1" => "data5-1", "index2" => "data5-2", "index3" => "data5-3")); $tpl->assign("array2", $array2); $tpl->display("test2.htm"); ?>
而模版的写法如下: templates/test2.htm:
(编辑:焦作站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|