Ad

OctoberCMS Global Page Properties?

Is it possible to set a series of global properties (such as social media usernames) that are available to all page views in OctoberCMS rather than having them associated to one CMS page or Static Page at a time?

For example, being able to use {{ twitter_username }} in any template, but it wouldn't show up as a field in any page form on the backend.

UPDATE: this can be achieved by registering a Twig function using registerMarkupTags in your plugin:

use System\Classes\PluginBase;

class Plugin extends PluginBase
{
    public function registerMarkupTags()
    {
        return [
            'functions' => [
                'globals' => function($var) {
                    switch ($var) {
                        case 'twitter_username':
                            return 'mytwitterusername';
                    }
                    return null;
                },
            ],
        ];
    }
}

In this case, calling {{ globals('twitter_username') }} from any template prints mytwitterusername.

Ad

Answer

Hmm yes better you need to add code to life-cycle method in layouts, so now page which are using that layout will have this info already loaded.

In layout code block you can use something like this

use RainLab\Pages\Classes\Page as StaticPage;

function onStart() {    
    $pageName = 'static-test'; // this will be static page name/filename/title
    $staticPage = StaticPage::load($this->controller->getTheme(), $pageName);
    $this['my_title'] = $staticPage->viewBag['title'];
    $this['twitter_username'] = $staticPage->viewBag['twitter_username'];

}

now inside your cms page you can use this variable

<h1>{{ my_title }} </h1>
<h3>{{ twitter_username }} </h3>

let me know if it you find any issues

Ad
source: stackoverflow.com
Ad