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.

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

But in magento 2, I couldn't do it.

Anyone know how to do this?

3 Answers 3


For magento 2 you have to do it using Helper and controller.

In Controller you have to write in Index.php

namespace MageArray\Blog\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
    protected $viewHelper;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \MageArray\Blog\Helper\Index\View $viewHelper,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->viewHelper = $viewHelper;
        $this->resultPageFactory = $resultPageFactory;

    public function execute()
        $page = $this->resultPageFactory->create(false, ['isIsolated' => true]);
        $this->viewHelper->prepareAndRender($page, $this);
        return $page;

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.

namespace MageArray\Blog\Helper\Index;
use Magento\Framework\View\Result\Page as ResultPage;
class View extends \Magento\Framework\App\Helper\AbstractHelper
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\Helper\Context $context,
    ) {
        $this->_scopeConfig = $scopeConfig;

    public function getStoreConfig($storePath){
        $StoreConfig =  $this->_scopeConfig->getValue($storePath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        return $StoreConfig;

    public function initProductLayout(ResultPage $resultPage,$layout_id)
        $post_list_layout = $this->getStoreConfig($layout_id);
        $pageConfig = $resultPage->getConfig();
        $update = $resultPage->getLayout()->getUpdate();
        $controllerClass = $this->_request->getFullActionName();
        return $this;

    public function prepareAndRender(ResultPage $resultPage, $controller)
        return $this;


make changes in namespace, module, helper and configPath names as per your requirements.

  • How can i change without using controller only from xml file Like in magento 1 Nov 25, 2015 at 7:45
  • How can I change the category page and product detail page layouts?
    – skynetch
    Dec 1, 2015 at 16:46

We can use something like below in Magento2:

<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="contact/contact/enabled" name="contact-us-link" />

Let me know if its helpful!


You can use "action" instruction with ifconfig like this:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceBlock name="category.products.list">
            <action method="setTemplate" ifconfig="path/to/config">
                <argument name="text" translate="true" xsi:type="string">namespace::module/list.phtml</argument>

"Action" is deprecated for now. Hope magento developers will add this feature to other instructions in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.