Magento dynamically changes layout using a system variable

Is it possible to change the structure of a Magento page (say, a product category page) dynamically using a system variable that was set on our own module? I want my default layout for my category to appear in my own module admin panel. So I don’t have to deal with these confusing XML layout files every time I want to change my default layout for a particular magenta page.

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

I see no way to pull the value of this system variable in the XML layout file.

But I'm sure there must be a proper way to do this. So far this is the closest key that I have

Magento - xml layouts, specify value for ifconfig?

But, nevertheless, I could not find a direct answer to what I really want to achieve

this is the contents 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> 
+5
source share
3 answers

This can be very easily achieved using the xml layout and the simple method in your helper. I do not see any requirements for an observer here or something else too complicated.

So, in accordance with your requirements for changing all layouts of a category page from your configuration value of your module storage, you will need the following in your xml layout:

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

Your module uses the following by default:

 public function switchTemplate() { $template = Mage::getStoreConfig('path_to/yourmodule/config'); return $template; } 
+10
source

we are talking about the root element pattern, so 3columns, 2columns, etc.? right?

Deploy an observer, listen to the controller_action_layout_generate_blocks_before event, and then get the block in the observer and set the pattern

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

That should do it.

Another idea, try the controller_action_layout_load_before event, but I think it is too early.

-1
source

In addition to Fabian's answer:

Perhaps you can expand the functionality of the category display modes. Using the controller_action_layout_load_before event, then extract the category display mode and create an XML update descriptor for it.

  $category = Mage::registry('current_category'); $handle = 'category_displaymode_' . strtolower($category->getDisplayMode()); $layout = $observer->getEvent()->getLayout(); $layout->getUpdate()->addHandle($handle); 

Thus, you can pre-define all types of layouts in your local.xml and easily switch between them by editing the "Display Mode" drop-down menu on the category editing page in the administrator.

With some settings in the administrator, you can add additional display modes to the drop-down menu to make all kinds of manipulations with XML versions available.

-2
source

Source: https://habr.com/ru/post/885519/


All Articles