我正在尝试了解使用 PHP 和 smarty 开发 Web 应用程序,这就是这本书
有一个文件部门列表.tpl
{* departments_list.tpl *}
{load_presentation_object filename="departments_list" assign="obj"}
{* Start departments list *}
<div class="box">
<p class="box-title">Choose a Department</p>
<ul>
{* Loop through the list of departments *}
{section name=i loop=$obj->mDepartments}
{assign var=selected value=""}
{* Verify if the department is selected to decide what CSS style to use *}
{if ($obj->mSelectedDepartment == $obj->mDepartments[i].department_id)}
{assign var=selected value="class=\"selected\""}
{/if}
<li>
{* Generate a link for a new department in the list *}
<a {$selected} href="{$obj->mDepartments[i].link_to_department}">
{$obj->mDepartments[i].name}
</a>
</li>
{/section}
</ul>
</div>
{* End departments list *}
我不明白上面文件中的一行是
{section name=i loop=$obj->mDepartments}
我是如何name=i
工作的,我初始化了什么,循环如何从我开始,
其次,上面的文件是使用文件function.load_presentation_object.php
<?php
// Plug-in functions inside plug-in files must be named: smarty_type_name
function smarty_function_load_presentation_object($params, $smarty)
{
require_once PRESENTATION_DIR . $params['filename'] . '.php';
$className = str_replace(' ', '',ucfirst(str_replace('_', ' ',$params['filename'])));
// Create presentation object
$obj = new $className();
if (method_exists($obj, 'init'))
{
$obj->init();
}
// Assign template variable
$smarty->assign($params['assign'], $obj);
}
?>
在上面的文件中,最后一行使用了参数 $params
$smarty->assign($params['assign'], $obj);
据我所知,从文件部门列表.tpl 将参数传递给 function.load_presentation_object.php 作为
{load_presentation_object filename="departments_list" assign="obj"}
所以当调用以下
function smarty_function_load_presentation_object($params, $smarty)
$params=depratments_list 和 $smarty=obj
那么如何 $smarty->assign($params['assign'], $obj);
工作上面的 $params['assign'] 是什么?