Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Can twig_vars be used in any file?

Started by Richard Hainsworth 8 years ago · 7 replies · 1090 views
8 years ago

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:

PHP
$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?

8 years ago

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

PHP
    // ... 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);
    }
// ...
last edited 04/16/18 by Oliver Scholz
8 years ago

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

8 years ago

@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.

8 years ago

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

8 years ago

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:
    PHP
    \\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

    PHP
    $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.

8 years ago

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

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 95 15 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago