October CMS resources and help articles

Simple and to the point. Optimized by the community.

Define a global variable on the CMS controller

3
by OFFLINE, last modified on March 30th, 2021

To define a variable that is available on your controller everywhere, you can use the following event listener:

\Event::listen('cms.page.init', function($controller) {
    $controller->vars['my_special_variable'] = 'some-value';
});

You can now access this variable in Twig or any PHP code section that has access to the CMS controller instance:

{{ my_special_variable }}

Page lifecycle example, works in Components as well:

function onStart()
{
    $special = $this->controller->vars['my_special_variable'];
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?