This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

bmcwilliams
bmcwilliams

I'm getting a cookie value in (the code section of) my "head" partial. I need to use this value in the body of all my layouts and in the footer partial. What is the best way to retrieve a cookie value in the head partial and make this value available to the layout and other partials?

mjauvin
mjauvin

I would say fetch the cookie in the LAYOUT php section and use it everywhere else...

bmcwilliams
bmcwilliams

yes, that seems to be the direction October pushes towards, but it would be sweet to do it once in the "head" partial, rather than in six layouts. Is there any way to do it this way?

Eoler
Eoler

bmcwilliams said: yes, that seems to be the direction October pushes towards, but it would be sweet to do it once in the "head" partial, rather than in six layouts. Is there any way to do it this way?

You can do it by handling CMS (frontend) events in your plugin and setting global Twig (page) variable, check listed events here: https://octobercms.com/docs/api/cms/page/beforedisplay

bmcwilliams
bmcwilliams

I'm not using a plugin. I just want to grab the cookie value in the code section of my "head" partial and make that value available to all layouts that include the "head" partial and other partials.

mjauvin
mjauvin

You can do this using the viewBag component... in your partial, set a viewBag property with the cookie value:

==
function onStart()
{
   $this->viewBag->setProperty('myVar', 'My Value');
}
==
... partial markup

Then use the value within your other partials / layout markup:

{{ viewBag.myVar }}

Hope that helps.

bmcwilliams
bmcwilliams

i think this is close, but this code snippet generates the error: "Call to a member function setProperty() on array"

== function onStart() { $this->viewBag->setProperty('myVar', 'My Value'); }

... partial markup

Last updated

mjauvin
mjauvin

Odd, that works for me.

mjauvin
mjauvin

try:

$this->getViewBag()->setProperty(...)

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.