我们使用 Sonassi在这里Mage_Page_Block_Template_Links
概述的方法扩展了 Magento 类。
编辑_construct
函数以包含我们的自定义模板:-
protected function _construct()
{
$this->setTemplate('page/template/links_nested.phtml');
}
编辑函数addLink
以包含$childMenu
变量并允许在 XML 布局中使用它:-
public function addLink($label, $url = '', $title = '', $prepare = false, $urlParams = array(), $position = null, $liParams = null, $aParams = null, $beforeText = '', $afterText = '', $childMenu = false)
{
if (is_null($label) || false === $label) {
return $this;
}
$link = new Varien_Object(array(
'label' => $label,
'url' => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
'title' => $title,
'li_params' => $this->_prepareParams($liParams),
'a_params' => $this->_prepareParams($aParams),
'before_text' => $beforeText,
'after_text' => $afterText,
'child_menu' => ($childMenu ? $this->getLayout()->getBlock($childMenu) : '')
));
$this->_links[$this->_getNewPosition($position)] = $link;
if (intval($position) > 0) {
ksort($this->_links);
}
return $this;
}
然后我们希望<childMenu>
在 local.xml 中为 topLinks 中的项目包含参数:-
<reference name="top.links">
<action method="addLink" translate="label title before_text">
<label>Account</label>
<url />
<title>Account</title>
<prepare />
<urlParams />
<position>10</position>
<liParams>id="account-dropdown"</liParams>
<aParams />
<before_text />
<after_text />
<childMenu>account-submenu</childMenu>
</action>
</reference>
然后将 childMenu 构造为account-submenu
:-
<reference name="top.links">
<block type="page/template_links" name="submenu" as="submenu">
<action method="setName">
<name>account-submenu</name>
</action>
<action method="addLink" translate="label title before_text">
<label>Contact Us</label>
<url />
<title>Contact Us</title>
<prepare />
<urlParams />
<position>110</position>
<liParams />
<before_text />
<after_text />
</action>
</block>
</reference>
然后我们对模板文件进行了一些修改以在childMenu
声明时呈现:-
<?php $_links = $this->getLinks(); ?>
<?php if(count($_links)>0): ?>
<ul class="links nav"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $count=>$_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()||$count): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?> link-<?php echo $count ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>>
<?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?>
<?php var_dump($_link->getChildMenu()); ?>
<?php echo ($_link->getChildMenu()) ? $_link->getChildMenu()->toHtml() : ''; ?>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
childMenu
除了在前端根本不呈现任何内容之外,一切都按预期工作,因此“我的帐户”顶部链接不包含子菜单。
childMenu
模板文件中的调用有什么不正确的吗?
<?php echo ($_link->getChildMenu()) ? $_link->getChildMenu()->toHtml() : ''; ?>