I want to know how to set the page layout dynamically based on an extension's admin configuration values.
I found how to change the page layout in the layout xml file, http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#layout_markup_columns, but couldn't find to do it based on admin configuration values.
In magento 1.x, I can do it.
<catalog_category_default>
<reference name="root">
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="one_column"><template>page/1column.phtml</template></action>
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="two_column_left"><template>page/2columns-left.phtml</template></action>
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="two_column_right"><template>page/2columns-right.phtml</template></action>
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="three_column"><template>page/3columns.phtml</template></action>
</reference>
</catalog_category_default>
But in magento 2, I couldn't do it.
Anyone know how to do this?
Best Answer
For magento 2 you have to do it using Helper and controller.
In Controller you have to write in Index.php
In namespace MageArray\Blog\Controller\Index;
Here MageArray is my NameSpace, Blog is my Module Name, Index is My controller Ans Class Name "Index" Is My action.
Now Create Helper File as i have View.php Helper and put code as below.
make changes in namespace, module, helper and configPath names as per your requirements.