Reputation: 23
I have a number of categories for which I need to use a different layout from my standard category layout. What is the best way to go about this without having to repeat XML code in the 'custom design' tab of the admin area? Each category I need to do this for is a 'brand', so I suppose that could be used as a common way for magento to recognize that the alternative template needs to be used?
Any help is appreciated at this point.
Thanks
Upvotes: 1
Views: 1025
Reputation: 1134
Another alternative is to use the "Page Layout" menu, though it requires a bit of extra work.
First, create a new extension to add the layout and an observer (there are numerous tutorials for creating an extension; eliminated here for brevity). In your new extension's config.xml
file (I'm using Bats_Coreextend
for reference):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Bats_Coreextend>
<version>0.1.0</version>
</Bats_Coreextend>
</modules>
<global>
<events>
<controller_action_layout_load_before>
<observers>
<addCategoryLayoutHandle>
<class>Bats_Coreextend_Model_Observer</class>
<method>addCategoryLayoutHandle</method>
</addCategoryLayoutHandle>
</observers>
</controller_action_layout_load_before>
</events>
<page>
<layouts>
<alt_category module="page" translate="label">
<label>Alt. Category (Desc. at bottom)</label>
<template>page/alt-category.phtml</template>
<layout_handle>page_alt_category</layout_handle>
</alt_category>
</layouts>
</page>
</global>
</config>
This will create the new layout and allow you to reference it in the menu as pictured above. We need the observer because unfortunately, even if the Page Layout is set (as shown above), you will not be able to reference the handle in your XML (e.g. catalog.xml)
To address this, create the observer function:
Create app/code/local/Bats/Coreextend/Model/Observer.php
In that file:
<?php
class Bats_Coreextend_Model_Observer extends Mage_Core_Model_Observer {
public function addCategoryLayoutHandle(Varien_Event_Observer $observer)
{
/** @var Mage_Catalog_Model_Category|null $category */
$category = Mage::registry('current_category');
if(!($category instanceof Mage_Catalog_Model_Category)) {
return;
}
if($category->getPageLayout()) {
/** @var Mage_Core_Model_Layout_Update $update */
$update = $observer->getEvent()->getLayout()->getUpdate();
/** NOTE: May want to add an additional
* conditional here as this will also cause the
* layout handle to appear on product pages that
* are within the category with the alternative
* layout.
*/
$update->addHandle($category->getPageLayout());
}
}
}
This will add the alt_category
handle to our layout so that it can be referenced and you can make the necessary changes to your page.
Finally, be sure to create the template file (i.e. page/alt-category.phtml
) as noted above.
Upvotes: 0
Reputation: 2334
If you know the ID of the category you could define all of your changes in your local.xml layout file like so, too:
<layout>
<CATEGORY_4>
<!-- updates here -->
</CATEGORY_4>
</layout>
Upvotes: 1
Reputation: 560
You could define a custom layout handle and call it in the specific categories.
First, define your layout handle (for example in your theme's local.xml):
<layout>
<my_awesome_update>
<block ..../>
</my_awesome_update>
</layout>
Then, in the backend's category edit page just enter into "Custom Layout Update":
<update handle="my_awesome_update" />
Upvotes: 4