0

I have several layouts, that share a common set of variables. At the moment I have hacked a solution together that gets the job done, but I am sure there is more elegant way to do it.

Here is how I currently share view variables between layouts

function onStart(){
    include_once 'themes/website/layouts/include_common_view_vars.php';
    /**
       Additional $this['lucky_poker'] params here for the specific layout
       */
}

where the include file has all my common addCSS and addJS files etc.

Any better ideas?

PS I read the Question about sharing variables --- and --- I did search the docs but didn't see what I wanted

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
David Lundquist
  • 690
  • 7
  • 19

2 Answers2

0

I am not sure it will solve you problem but some different approach can be helpful.

what else we can do is.

  1. Create New Plugin / or if you have related plugin use that one
  2. Define Component like "SharedVars" or something
  3. you need to include it in your layouts (all layout in which you need to access variables)

Now in component

public function onRun() {
    // or your file which can return values then loop through it
    // set variables in page
    $this->page['testVar'] = 'Test';
}

now variable "testVar" will be available in all your partials and through out your all page which use this layout.

Value : {{ testVar }}

for simplicity you can assign values/variables in array (sharedVars) and assign to page.

this way next time you create new layout and you need to add that component in layout .. and just for safe play we include component before we use {% page %} in layout.

Its another way I found to share same variables.

Note: In order to work component, It must be in layout not in partials as partials are not included in Page execution life cycle.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
0

I think the best option would be to use a theme information file which has support for the form where you can add all necessary data (via theme settings ui) to be shared between all layouts.

https://octobercms.com/docs/themes/development#theme-information

Edgars Ozolins
  • 298
  • 5
  • 11