0

我们使用 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() : ''; ?>
4

2 回答 2

1

在布局 XML 中,您应该先定义子菜单块(<block>节点),然后再将其添加到顶部链接(<action method="addLink"><reference name="top.links">)。由于 Magento 在节点到来时对其进行处理,因此他不会知道该块是否存在

于 2013-05-17T13:21:18.240 回答
0

对于任何对此有任何问题的人-我想我会发布我的解决方案。

我认为将核心 Page/Block/Template/Links.php 文件从 app/code/core 复制到 app/code/local 并对该文件进行调整(如果此 Links.php 文件在将来的 Magento 更新中更新,您需要确保您的覆盖符合核心中的新更新文件)。

无论如何,这里的问题肯定是说明缺少一个步骤 - 一切都是有意义的,直到他们引用(无处不在)块“mymainmenu.links” - 这个块在任何地方都不存在,因此它不会'不工作。如果您继续将链接添加到“top.links”块,Magento 将继续使用默认块来显示您的链接。所以这里明显的答案是创建一个新块。

在您的布局文件中添加以下块(我正在使用 local.xml)app/design/frontend/( package )/( theme )/layout.xml

<?xml version="1.0"?>
    <layout version="0.1.0">
        <default>
            <reference name="header"> <!-- You could also use root here to give it a larger scale access -->
                <block type="templatelinks/page_template_links" name="mymainmenu.links" as="mymainmenuLinks">
                    <action method="setName">
                        <name>mymainmenu-links</name>
                    </action>

                    <action method="addLink" translate="label title">
                        ....
                    </action>
                </block>
            </reference>
        </default>
    </layout>

将块类型设置为自定义模块名称)/(块位置实例化我们的自定义模块并通过它处理我们的操作。

您还必须记住在我们的模板中显示您新创建的(在本例中为 mymainmenuLinks)块 - 在大多数情况下,它将是您的应用程序/设计/前端/()/(主题)/模板/页面/html/header.phtml文件

定位

<?php echo $this->getChildHtml('topLinks') ?>

并替换为您的新块名称

<?php echo $this->getChildHtml('mymainmenuLinks') ?>
于 2013-09-15T23:29:02.907 回答