我目前正在尝试将侧栏车添加到我正在研究的网站的标题中,但是它似乎不起作用。这是我的文件:

page.xml:

<block type="page/html_header" name="header" as="header">
    <block type="page/template_links" name="top.links" as="topLinks"/>
    <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
    <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>
    <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
        <label>Page Header</label>
        <action method="setElementClass"><value>top-container</value></action>
    </block>
    <block type="checkout/cart_sidebar" name="cart_sidebar" as="topcart" template="checkout/cart/sidebar.phtml"/>
</block>

header.phtml:

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

目前,我在Magento中关闭了缓存,但是我尝试在System-> Cache Management中刷新缓存。有任何想法吗?

有帮助吗?

解决方案

您定义的块的布局名称(cart_sidebar)与此块的默认布局名称相同。布局名称必须在页面的整个布局中是唯一的,但是别名(topcart)在父块中只需要唯一。

除非您在其布局文件中使用此布局名称评论了其他块,或者更好地将其从布局中删除 unsetChild 您自己的local.xml(或模块)布局文件中的块方法,然后您无法使用该布局名称添加一个块。

<action method="unsetChild"><name>cart_sidebar</name></action>

请注意,使用布局 remove 标签不允许您添加带有相同布局名称的块,该块仍然存在于布局中,而不是输出。

<remove name="cart_sidebar" />

还请注意 unsetChild 必须从父块中调用 remove 标签可以直接在布局手柄下应用。

最后,page.xml只能定义页面的基本布局结构,您不应将新块直接添加到此布局文件中,而应引用在其他布局文件中创建的块并在此处添加自己的块。

<reference name="header">
    .........
</reference>

其他提示

我会在另一个答案中回答您的评论,因为该评论没有足够的空间。

布局句柄定义将添加哪个页面,因此,如果您进一步查找 page.xml 在您发布的部分上方,您会看到 <default> 标签。这是打开布局句柄标签和使用 默认 方法 在此处将布局更改应用于每个页面.

如果您查看其他布局文件,您将在 catalog.xml 你发现 <catalog_product_view> 这是产品视图页面的布局手柄和此手柄内定义的任何布局项目,仅在查看产品时才使用。布局句柄是由模块名称,控制器名称和操作名称的下划线分离的串联创建的,因此,使用上述模块是目录,控制器是该控制器中的产品和操作。

这为您提供了仅从您自己的布局文件上的特定页面上进行布局更改所需的范围。值得注意的是,如果您不确定如何确定特定页面的布局句柄,则默认将其插入为第一类 <body> 每个页面上的HTML标签。

请注意,前端和管理页面具有单独的布局文件,因此 <default> 标签仅适用于前端,或依赖于将其放置在哪个布局文件中的管理员。

我强烈建议您创建自己的 local.xml 布局文件(或者如果要创建扩展名,则在其中定义了自己的布局文件 config.xml)。使用 local.xml 是管理布局的更干净的方法 - 您将默认布局文件不受欢迎,并通过单个文件使用手柄管理所有布局更改。如果您的主题布局目录中不存在local.xml,则只需创建它,它将自动拾取。这里有一个 local.xml 带有一些示例语法的布局文件使您开始:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="content">
            <block type="page/html" name="new.block" after="-" template="module/block/template.phtml" />
        </reference>
    </default>
    <catalog_product_view>
        <reference name="right">
            <action method="unsetChild">
                <name>child.block.name</name>
            </action>
        </reference>
        <remove name="some.block.name" />
        <block type="page/html" name="other.new.block" template="module/block/other_template.phtml" before="block_name_to_appear_before" parent="content" />
    </catalog_product_view>
</layout>
许可以下: CC-BY-SA归因
scroll top