Magento layout handle name is misleading

在 Magento app/design/frontend/base/default/layout/catalog.xml 文件里有一段让我费解的配置:

<catalog_category_layered_nochildren translate="label">
    <label>Catalog Category (Without Subcategories)</label>
    <remove name="right.reports.product.viewed" />
    <reference name="right">
        <block type="reports/product_viewed" before="right.permanent.callout" name="left.reports.product.viewed" template="reports/product_viewed.phtml" />
    </reference>
</catalog_category_layered_nochildren>

catalog_category_layered_nochildren,从字面上看,应该是只对 is_anchor == 1 并没有子目录的目录有效;但事实上,它对所有没有子目录的目录有效,所以我想,handle 应该取名 catalog_category_nochildren 更合适。Mage_Catalog_CategoryController::viewAction() 里有一段

if (!$category->hasChildren()) {
    $update->addHandle('catalog_category_layered_nochildren');
}

由此可见这里仅检验 hasChildren,未检验 getIsAnchor。

catalog.xml 里把 right.reports.product.viewed 移除又添加,重新添加后 block 叫 left.reports.product.viewed,但实际位置仍在右边。这么做有什么意义?我费神地看了半天,得出的结论是:

没有意义。大概 Magento 是借此演示一下 layout syntax。

虽然没有意义,但是影响还是有的,有这一段,nochildren 的 category page sidebar 排序为 Compare, Compared, Viewed;否则排序为 Compare, Viewed, Compared。

Sidebar in the sequence of Compare, Compared, Viewed
Sidebar in the sequence of Compare, Compared, Viewed
Sidebar in the sequence of Compare, Viewed, Compared
Sidebar in the sequence of Compare, Viewed, Compared

2 Replies to “Magento layout handle name is misleading”

Leave a Reply

Your email address will not be published. Required fields are marked *