【问题标题】:Custom layout for category page - handle trouble类别页面的自定义布局 - 处理麻烦
【发布时间】:2015-09-18 12:57:21
【问题描述】:

我有自定义模块,我在其中创建了新布局

<config>
<global>
    <page>
        <layouts>
            <category_special translate="label">
                <label>Limited category</label>
                <template>page/limited_category.phtml</template>
                <layout_handle>special_limited</layout_handle>
            </category_special>
        </layouts>
    </page>
</global>

Limited 类别出现在下拉列表中,我选择了它。 在 local.xml 我写了

<special_limited translate="label">
    <label>Category - Limited</label>
    <reference name="content">
        <block type="catalog/category_special" name="category.products" template="catalog/category/special.phtml">

        </block>
    </reference>
</special_limited>

布局可以使用limited_category.phtml模板加载页面,但句柄不是。看起来 catalog_category_default 在这个页面上

【问题讨论】:

    标签: xml magento layout module block


    【解决方案1】:

    The answer

    如上面链接中所述 - 类别控制器不调用 Mage_Page_Helper_Layout::applyHandle(),因此它的自定义句柄不适用。要修复它,可以覆盖 Mage_Catalog_CategoryController::viewAction():

    public function viewAction(){
       ....
       $this->addActionLayoutHandles();
       $update->addHandle($category->getLayoutUpdateHandle());
       $update->addHandle('CATEGORY_' . $category->getId());
       if ($settings->getPageLayout()) {
                $this->getLayout()->helper('page/layout')->applyHandle($settings->getPageLayout());
            }
       ...
    }
    

    【讨论】:

      【解决方案2】:

      这是大多数 Magento 开发人员所拥有的常见 mus-interpret 的完美示例。所以我在这里尝试更多地探索。

      实际上 CMS 页面呈现(例如:主页)和类别页面是唯一呈现它的页面。意味着这两个页面都手动生成布局更新句柄,然后加载和呈现它们。 Magento 的默认行为与这些页面完全不同。这就是 Magento 处理普通控制器操作的方式。

      public function someAction()
      {
          $this->loadLayout();
          $this->renderLayout();
      }
      

      $this-&gt;loadLayout() 生成布局更新句柄然后加载它们。对于 CMS 页面和类别页面,您可以看到此类调用不存在。相反,他们将通过他们的控制器手动准备布局。我认为我的观点现在更清楚了。

      现在让我们来看看loadLayout()在这里做了什么。

      public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true)
      {
          // if handles were specified in arguments load them first
          if (false!==$handles && ''!==$handles) {
              $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
          }
      
          // add default layout handles for this action
          $this->addActionLayoutHandles();
      
          $this->loadLayoutUpdates();
      
          if (!$generateXml) {
              return $this;
          }
          $this->generateLayoutXml();
      
          if (!$generateBlocks) {
              return $this;
          }
          $this->generateLayoutBlocks();
          $this->_isLayoutLoaded = true;
      
          return $this;
      }
      

      在此方法中,前两行用于生成布局更新句柄。因此,让我们专注于这些行。这是第一行代码。

      if (false!==$handles && ''!==$handles) {
          $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
      }
      

      在正常情况下,这将添加 default 布局更新句柄。现在让我们看看$this-&gt;addActionLayoutHandles() 做了什么:

      public function addActionLayoutHandles()
      {
          $update = $this->getLayout()->getUpdate();
      
          // load store handle
          $update->addHandle('STORE_'.Mage::app()->getStore()->getCode());
      
          // load theme handle
          $package = Mage::getSingleton('core/design_package');
          $update->addHandle(
              'THEME_'.$package->getArea().'_'.$package->getPackageName().'_'.$package->getTheme('layout')
          );
      
          // load action handle
          $update->addHandle(strtolower($this->getFullActionName()));
      
          return $this;
      } 
      

      它实际上添加了 3 种布局更新句柄。他们是

      1. 存储特定布局更新句柄。例如:STORE_default

      2. 主题和区域特定的布局更新句柄。例如:THEME_frontend_rwd_default

      3. 动作布局更新句柄例如:catalog_category_view 用于类别页面。

      因此,如果我们总结一下,在正常情况下,Magento 基本上会生成 4 个布局更新句柄1它不会生成 layout_handle em> 特定的布局更新句柄。

      那么layout_handles的应用是什么?

      看起来,它们通常是为通过布局更新 XML 文件更新页面布局的快捷方式定义的。即执行这样的操作

      <layout>
          <{some_handle}>
              <update handle="page_one_column" />
          </{some_handle}>
      </layout>
      

      除此之外,这些句柄没有特定用途。所以你不应该使用这些句柄来做一些布局定制。为此,您需要使用 Magento 默认生成的其他布局更新句柄。在我上面指定的 4 个布局更新句柄中,动作布局更新句柄应该是进行修改的理想场所。

      在这种情况下我们能做什么

      在这种情况下,您的 local.xml 应该看起来像

      <layout>
          <catalog_category_view>
              <reference name="content">
                  <block type="catalog/category_special" name="category.products" template="catalog/category/special.phtml" />
              </reference>
          </catalog_category_view>
      </layout>
      

      因此您不需要任何重写。您可以通过单个布局更新 xml 文件包含一个块。


      1 : 默认情况下,Magento 会生成其他布局更新句柄。它使用特殊事件controller_action_layout_load_before 来执行此操作。

      【讨论】:

        猜你喜欢
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 2021-02-25
        • 2015-05-24
        • 2015-02-18
        • 2021-04-04
        • 2018-11-25
        • 2014-02-11
        相关资源
        最近更新 更多