Skip to content

Instantly share code, notes, and snippets.

@dimayakovlev
Created March 20, 2016 09:46
Show Gist options
  • Save dimayakovlev/0d998aa96bc53647120e to your computer and use it in GitHub Desktop.
Save dimayakovlev/0d998aa96bc53647120e to your computer and use it in GitHub Desktop.
GravExampleAddVariableToTwig

Create a new plugin folder in user/plugins/example, and add those files:

user/plugins/example/example.php user/plugins/example/example.yaml

Then in your twig, you'll have the users object available:

{{ dump(users) }}

<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
class ExamplePlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
];
}
/**
* Make var accessible from twig.
*/
public function onTwigSiteVariables()
{
if (!$this->active) {
return;
}
$users = .... /* get users data from the user/accounts folder */
$this->grav['twig']->twig_vars['users'] = $users;
}
}
enabled: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment