листинг departments_list.php
// Manages the departments list
class DepartmentsList
{
/* Public variables available in departments_list.tpl Smarty template */
public $mSelectedDepartment = 0;
public $mDepartments;
// Constructor reads query string parameter
public function __construct()
{
/* If DepartmentId exists in the query string, we're visiting a
department */
if (isset ($_GET['DepartmentId']))
$this->mSelectedDepartment = (int)$_GET['DepartmentId'];
}
/* Calls business tier method to read departments list and create
their links */
public function init()
{
// Get the list of departments from the business tier
$this->mDepartments = Catalog::GetDepartments();
// Create the department links
for ($i = 0; $i < count($this->mDepartments); $i++)
$this->mDepartments[$i]['link_to_department'] =
Link::ToDepartment($this->mDepartments[$i]['department_id']);
}
}
листинг departments_list.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 *}
листинг store_front.tpl
{* smarty *}
{config_load file="site.conf"}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>{#site_title#}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" href="styles/tshirtshop.css" />
</head>
<body>
<div id="doc" class="yui-t2">
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<div id="header" class="yui-g">
<a href="index.php">
<img src="images/tshirtshop.png" alt="tshirtshop logo" />
</a>
</div>
<div id="contents" class="yui-g">
Place contents here
</div>
</div>
</div>
<div class="yui-b">
Place list of departments here
{include file="departments_list.tpl"}
</div>
</div>
</div>
</body>
</html>
листинг smarty_function_load_presentation_object
// 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);
}