Fati Alami
Fati Alami

Reputation: 51

how to change dynamically the page layout in octobercms

I would like to change the layout of a page dynamically in octobercms

it means that from the value of a session the layout of the page must change

I made this code but it does not work:


==
<?php

function onInit()
{
   $this.layout.id = default;
}

?>
==_

so how to do it ? thank you in advance

Upvotes: 1

Views: 819

Answers (2)

golammostofa
golammostofa

Reputation: 1

write the page layout this way $page->layout = 'your-layout-here';

public function boot()
{
    \Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
        if (!$page || $url === '404') {
            return $page;
        }

        // Assign the page's layout with your selected layout
        $page->layout = 'your-layout-here';

        return $page;
    });
}

Upvotes: 0

LukeTowers
LukeTowers

Reputation: 1291

You would be able to accomplish this by hooking into the cms.page.beforeDisplay event: https://octobercms.com/docs/api/cms/page/beforedisplay

You will need to implement this in a custom plugin as all of the theme page / layout functions run too late in the cycle to have the effect you need. (see https://octobercms.com/docs/plugin/registration for more information on how to get started building a plugin).

You can use the following code as an example of how to achieve what you are looking for:

public function boot()
{
    \Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
        if (!$page || $url === '404') {
            return $page;
        }

        // Assign the page's layout with your selected layout
        $page->layout = \Cms\Classes\Layout::loadCached(\Cms\Classes\Theme::getActiveTheme(), 'your-layout-here');

        return $page;
    });
}

Upvotes: 2

Related Questions