Can twig_vars be used in any file?


#1

I can’t get $this->grav['twig']->twig_vars() to work. (I can’t even find the function in Grav API documentation).

I have tried to duplicate code in the login and form plugins.

I want to create a protect variable $myvar in a plugin class. Then I want to update $myvar in a custom form, eg., $this->myvar['operator'] = $data['operator'] (inside onFormProcessed(). This all works as expected and I can see $myvar in a debugger bar after dumping it.

Now I want to render the new value in twig, eg., in default.md I have the content line {{ myvar.operator }}

So I thought if I put the following in onTwigSiteVariables:

$this->myvar['operator'] = 'Some name';
$this->grav['twig']->twig_vars('myvar') = $this->myvar;

However, I am not seeing anything rendered by twig. I have twig processing on for pages. I have tried to put {{ myvar.operator }} in a template.

What am I missing?


#2

I had this recently. It seems that you need to use the method $this->grav['twig']->twig->addGlobal():

    // ... Inside Plugin Class
    public $myVar;

    // You need to enable this in getSubscribedEvents()

    public function onTwigExtensions()
    {
        if ($this->isAdmin()) {
            return;
        }

        $this->grav["twig"]->twig->addGlobal('myVar', $this->myVar);
    }
// ...

#3

Or as follows:

// Load count data into a twig variable
$this->grav['twig']->twig_vars['viewcounts'] = $data;

#4

Interesting! Is this equivalent? So, @finanalyst, did you make the variable protected? It needs to be public.


#5

@Utis, I made the variable ‘protected’ because that is the way it was done for ‘login’ and ‘form’ plugins. I will try the public route as well.


#6

I tried this approach, but couldn’t access the data inside a twig. I’m not sure why it didn’t work.


#7

Thanks @Utis and @Perlkonig for the help.

  1. I found that some of my problems were eliminated when I tried the plugin on a clean Grav skeleton with minimal other plugins. I’m not sure what causes the problems in my full site, but now I have got something working.
  2. The function twig_vars may not be entirely what I need. There is a problem about order. If the call to twig_vars is before the assignment to the variable, then any new assignment is not picked up. Thus:
\\in plugin class
public $myVar;
\\ in onPluginInitialized
$this->grav['twig']->twig_vars['myVar'] = $myVar;
$this->myVar['one'] = 'something interesting';
\\ in default.md
{{ myVar.one }}

This does not produce any output. But

$this->myVar['one'] = 'something interesting';
$this->grav['twig']->twig_vars['myVar'] = $myVar;

generates something interesting.

I realize that this can be overcome by placing the twig_vars reference in another callback. However, it seems that twig_vars is putting a copy of the variable into the twig area, and not a reference to the variable that can be updated.

Eg. putting the twig_vars call into onTwigExtensions works as desired.

  1. $this->grav['twig']->addGlobal('myVar', $this->myVar) does not work in onPluginsInitialized.

  2. I don’t know whether twig_vars[] and twig->addGlobal() are equivalent.


#9

[Post deleted, because on second reading I realized that you’re already aware of onTwigExtensions.]