Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento changes layout dynamically via system variable

Is there a way we could changes the layout of a Magento page (let's say a product category page) dynamically by using system variable which have been set on our own module? I want to be able to set my category page's default layout via my own module admin config panel. So that I don't have to deal with those confusing XML layout file each time I want to change my default layout for a certain magento page.

I know, on a phtml file, we could simply call our own module's system variable by calling Mage::getStoreConfig('module/scope/...') to use that system variable. but what if we want to use that system variable to change the whole layout which is set on the XML layout file by default.

I don't see any ways to pull that system variable value on the XML Layout file.

But I'm pretty sure there must be a right way to do that. So far, this is the closest clue that I've got

Magento - xml layouts, specify value for ifconfig?

But, still, I couldn't find any direct answer for what I really want to achieve

this is the content of my config.xml

<config>
    <modules>
        <Prem_Spectra>
            <version>0.1.0</version>
        </Prem_Spectra>
    </modules>

    <global>
        <models>
            <spectra>
                 <class>Prem_Spectra_Model</class>
            </spectra>
        </models>

        <helpers>
            <prem_spectra>
                <class>Prem_Spectra_Helper</class>
            </prem_spectra>
        </helpers>

    </global>
</config>
like image 570
Kamal Avatar asked Jul 21 '12 15:07

Kamal


1 Answers

This can be very easily achieved using layout xml and a simple method in your helper. I don't see any requirement for an observer here or anything else overly elaborate.

So, based on your requirements to change all category page layouts from your own modules store config value you will require the following in your layout xml:

<catalog_category_view>
    <reference name="root">
        <action method="setTemplate">
            <template helper="yourmodule/switchTemplate" />                  
        </action>
    </reference>
</catalog_category_view>

And the following in your modules default helper:

public function switchTemplate()
{
    $template = Mage::getStoreConfig('path_to/yourmodule/config');
    return $template;
}
like image 175
Drew Hunter Avatar answered Oct 30 '22 16:10

Drew Hunter