0

Mage_Page_Block_Template_LinksSonassi here で概説されているメソッドを使用して、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>、topLinks のアイテムの local.xml にパラメーターを含めます。

<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">within <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 ) ブロックをテンプレートに表示することも忘れないでください。ほとんどの場合、それはapp/design/frontend/( package )/( theme )/template/になります。 page/html/header.phtmlファイル

見つける

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

新しいブロック名に置き換えます

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