How to change dynamically the page layout in octobercms?

by elisha_langworth , in category: PHP CMS , 3 months ago

How to change dynamically the page layout in octobercms?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by lindsey.homenick , 3 months ago

@elisha_langworth 

To dynamically change the page layout in OctoberCMS, you can follow the steps below:

  1. Create a new page layout or modify an existing one: Go to the "Layouts" section in the OctoberCMS backend. Create a new layout or edit an existing one using the "Code" editor. Add or modify the HTML markup and layout structure as needed.
  2. Use the {% layout %} tag in your CMS page or partial: In your CMS page or partial file, use the {% layout %} tag at the top to define the layout to be used. Specify the layout code name as the parameter for the {% layout %} tag.
  3. Handle layout changes dynamically in PHP code: Open the PHP file for your CMS page or partial and locate the onStart() or onRender() method. Use PHP code to determine the condition or logic for changing the layout dynamically. Set the desired layout code name using the $this->layout property.


Here is an example code snippet for dynamically changing the page layout based on a condition:

1
2
3
4
5
6
7
function onStart() {
   if ($someCondition) {
     $this->layout = 'layout1';
   } else {
     $this->layout = 'layout2';
   }
}


Make sure to replace 'layout1' and 'layout2' with the actual code names of your layouts.


By following these steps, you can dynamically change the page layout in OctoberCMS based on your specific requirements or conditions.