导航菜单:page.xml 不调用重写的类(topmenu.php)
-
12-12-2019 - |
题
编辑 :由乔纳森·赫西解决。如果您想帮助我覆盖模板和布局,请参阅帖子末尾(或下面)的另一个问题:)
附属问题 :我可以在模块中覆盖 page.xml、topmenu.phtml 和 renderer.phtml 还是必须在主题文件夹中手动替换它们?如果是这样,您介意引导我学习教程或快速解释一下吗?我似乎不容易找到符合我的情况的教程......
我想覆盖 topmenu.phtml、renderer.phtml 和 page.xml 模板, 在我的模块内, , 如果可能的话。
感谢您的进一步帮助:)
我是一个新的 Magento 开发人员,所以我尝试做简单的事情,而我对 Magento 的理解是有限的......
我想要做的是通过创建我自己的模块,以另一种方式(面板式下拉菜单)显示导航菜单(顶部)。我查看了当前菜单的定义/调用位置并找到了该文件 design/frontend/{mytheme}/default/layout/page.xml
看起来像:
<default translate="label" module="page">
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
<block type="page/html_header" name="header" as="header">
<block type="core/text_list" name="top.menu" as="topMenu" translate="label">
<label>Navigation Bar</label>
<block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml">
<block type="page/html_topmenu_renderer" name="catalog.topnav.renderer" template="page/html/topmenu/renderer.phtml"/>
</block>
</block>
</block>
</block>
</default>
(还有很多其他的东西,但这是必不可少的)
<block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml">
<block type="page/html_topmenu_renderer" name="catalog.topnav.renderer" template="page/html/topmenu/renderer.phtml"/>
</block>
这些行表明我们需要该类 Mage_Page_Block_Html_Topmenu
, ,显示在模板文件中 design/frontend/{mytheme}/default/template/page/html/topmenu.phtml
, , 其中包含 :
<nav id="nav">
<div class="nav-megadropdown col6">
<?php echo $this->getHtml('level-top'); // $this is the a Topmenu object ?>
</div>
<div style="clear:both"></div>
</nav>
方法 getHtml
到达子块 catalog.topnav.renderer
并得到它的类(Mage_Page_Block_Html_Topmenu_Renderer
并使用模板调用 render 方法 design/frontend/{mytheme}/default/template/page/html/topmenu/renderer.phtml
其中包含一个复杂但有效的菜单(我通过修改 Mage 类和主题 phtml 文件进行了尝试,并且成功了).
但现在,我想通过覆盖我的模块中的类(它是活动的并且看起来像 Fqu_Supergroup
)。所以我从 config.xml 开始:
<config>
<modules>
<Fqu_Supergroup>
<version>0.1.0</version>
</Fqu_Supergroup>
</modules>
<global>
<blocks>
<Page>
<rewrite>
<html_topmenu>Fqu_Supergroup_Block_Html_Topmenu</html_topmenu>
</rewrite>
</Page>
</blocks>
</global>
</config>
还有我的班级(Fqu_Supergroup_Block_Html_Topmenu
)在我的模块(local/Fqu/Supergroup/Block/Html/Topmenu.php
) 仅包含重写方法(_getRenderedMenuItemAttributes 和 _getMenuItemClasses)
所以在这里,当我尝试我的代码时,页面显示一个损坏的菜单,因为它没有使用我的类(我 var_dump'ed 该类(get_class($this)
)它用于渲染菜单并返回 Mage_Page_Block_Html_Topmenu
);当我 var_dump 包含的文件(get_included_files()
)我根本没有看到任何对我的模块的引用(尽管它在管理面板中激活)...
很抱歉这篇文章很长,但感谢您的关注和帮助!
附属问题 :我可以在模块中覆盖 page.xml、topmenu.phtml 和 renderer.phtml 还是必须在主题文件夹中手动替换它们?
解决方案
虽然我没有详细分析你的帖子,但在浏览上面的内容时我发现的第一件事是你声明你的重写不正确, Page
应该是 page
. 。该字符串与模块名称不匹配,即这 Page
在 Mage_Page
, ,它而是匹配在您正在重写的模块中声明资源时使用的字符串,因此在这种情况下 config.xml
为了 Mage_Page
核心模块:
...
<blocks>
<page> <!-- this is the string to match -->
<class>Mage_Page_Block</class>
</page>
</blocks>
...
通过引用 page
与模块的匹配 Mage_Page
通过检查全局配置(基本上是一个包含所有配置的大型 xml 文件)来确定 config.xml
所有模块的文件)。字符串的其余部分根据类名确定,因此 html_topmenu
映射到类 Mage_Page_Block_Html_Topmenu
重写将应用于的类。
对于您的额外问题,是的,您可以通过声明模块的布局文件来覆盖任何布局文件或模板。在其中,您可以声明所需的任何布局更改,包括更改块的模板,这些更改将优先于核心布局文件,因为您的模块将始终在所有核心模块之后加载。