Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Theme variables

Started by Muut Archive 10 years ago · 2 replies · 619 views
10 years ago

Hi,

I am trying to pass some variables into my theme. I have setup my theme class file like follows:

PHP
<?php
namespace Grav\Theme;

use Grav\Common\Theme;

class MyTheme extends Theme
{
    public static function getSubscribedEvents()
    {
        return [
            'onThemeInitialized' => ['onThemeInitialized', 0]
        ];
    }

    public function onThemeInitialized()
    {
        if ($this->isAdmin()) { 
            $this->active = false;
            return;
        }

        $this->enable([
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
        ]);
    }

    public function onTwigSiteVariables()
    {
        $this->grav['twig']->myVariable = 'test';
    }
}

I would like to be able to access the variable myVariable within my theme's header.

{{ myVariable }} doesn't seem to work. What am I doing wrong?

Thanks,

10 years ago

I was able to achieve this by $this->grav['twig']->twig->addExtension(new Test());

You also need to have process: twig: true in your YAML file which I didn't realise. I still don't know how to just pass a variable over but this is just as good for my purpose.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Muut Archive, 10 years ago
1 676 1 week ago
Archive · by Deleted User, 9 years ago
0 3043 9 years ago
Archive · by Muut Archive, 9 years ago
2 1983 9 years ago
Archive · by Muut Archive, 9 years ago
2 5338 9 years ago
Archive · by Muut Archive, 9 years ago
1 4135 9 years ago