Use one variable in several Snippets

Hi everybody,

small Question, I have a variable defined in my Snippet header.php, let’s say $sidebar = 1, now I want also to use this variable in my snippet footer.php. But a error appears that the variable is not defined. Any workaround for this?

Cheers, Oli

Out-of-the-box, this is not possible. Discussion of workarounds has been going on for a while now, and there are some interesting possibilities to approach this. One of them is @jenstornell’s Kirby Starthook plugin.

1 Like

What is your use case, @Oli1? Depending on that, the best approach might vary.

Thanks for your answers … @texnixe, I have a global Blueprint setting which I load in all blueprints for “activate sidebar”, problem is that in header.php, there’s a switch for Bootstrap class COL-SM-9 or COL-SM-12 and in footer.php there’s the rest, if {activate sidebar == 1} { ... } else { ... } and in some other case I need this also in the menu.php Snippet.

Not sure why this doens’t work, the two workarounds seems not to be the best solutions. In my first tryouts with Kirby I have created a variables.php which seems to be the best solution, but I can’t get access to them (loaded before all other snippets). Would be a great feature, in most CMS this works.

Maybe there’s a little workaround expect of a Plugin or global setting. It’s must change depend of the blueprint settings.

Yes, in this case you don’t need a global setting because this information is page specific.

But since you then have this information in every page, you can check for that page setting:

if($page->sidebar()->bool()); // assuming this field is called sidebar
  //do stuff
endif;

No need to define a variable first.

Thanks! Yes it’s possible, but of course very intense to work with. I need this function very often in the Template, and so in this case I have to define it in all snippets. But it will work, yes :slight_smile:

Didn’t you say you only need this in max three snippets, header, footer and maybe menu? You could of course create a function, but what do you want the function to return, a class? All possible…

Exact. Only thought it was easier but you’re right, it’s no problem to use this Codesnippet :slight_smile:
The return of a class makes not really sense :wink: