Magento changes layout dynamically via system variable

假装没事ソ 提交于 2020-01-13 02:23:51

问题


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>

回答1:


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;
}



回答2:


we are talking about the template of the root-element, so 3columns, 2columns, etc? correct?

Implement an observer, listen to the event controller_action_layout_generate_blocks_before and then get the block in the observer and set the template

Mage::app()->getLayout()->getBlock('root')->setTemplate($myFancyTemplatePath);

This should do it.

Other idea, try the event controller_action_layout_load_before, but I think this is too early.




回答3:


In addition to Fabian's answer:

You could perhaps extend the functionality of the category 'display modes'. Using the controller_action_layout_load_before event and then retrieve the display mode of the category and create a XML update handle for it.

    $category = Mage::registry('current_category');
    $handle = 'category_displaymode_' . strtolower($category->getDisplayMode());

    $layout = $observer->getEvent()->getLayout();
    $layout->getUpdate()->addHandle($handle);

This way you can pre-define all kinds of layouts in your local.xml and easily switch between them by adjusting the 'display mode' dropdown on the category edit page in the admin.

With some tweaking in the admin you can add additional display modes to the dropdown to make more types of custom display mode xml update handles available.



来源:https://stackoverflow.com/questions/11593340/magento-changes-layout-dynamically-via-system-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!