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.

Plugins

Set Variable in Twig Extension Class

Solved by Werner Joss View solution

Started by Werner Joss 7 years ago · 1 replies · 1107 views
7 years ago

I'm currently writing a Plugin which parses an ics (calendar-) File and creates an events list which is made available via a twig variable, according to https://learn.getgrav.org/15/cookbook/plugin-recipes .
This works ok so far (with hardcoded values), but now I want to pass variable value(s) to my TwigExtension Class.
Unfortunately, there seems to be no way, so far.
Normally, I would have a class like
class Testclass
{
private $testvar = "default value";
public function setTestvar($testvar) {
$this->testvar = $testvar;
}
}
$Testclass = new Testclass();
// now pass a value to $testvar in Testclass:
$Testclass->setTestvar("another value");

but I can't see how this could be done with my TwigExtension, which is initialized like this:
$this->grav['twig']->twig->addExtension(new EventListTwigExtension());

anyone got a hint for me ?

👍 1
7 years ago Solution

ok, solved 😉
I had to:

  • create new TwigExtension Object $ext
  • set desired variable Value
  • then add Extension: $this->grav['twig']->twig->addExtension($ext);

-> ok.

👍 1

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 42 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 71 2 months ago