Gets the registered Globals.
public getGlobals ( ) : array | ||
return | array | An array of globals |
$loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig = new Twig_Environment($loader); // Set a global variable $twig->addGlobal('site_title', 'My Website'); // Get all global variables $globals = $twig->getGlobals(); // Print global variables foreach ($globals as $key => $val) { echo $key . ' => ' . $val . '
'; }
$loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig = new Twig_Environment($loader); // Set global variables $twig->addGlobal('site_title', 'My Website'); $twig->addGlobal('menu_items', array('Home', 'About', 'Contact')); // Get a specific global variable $site_title = $twig->getGlobals()['site_title']; echo $site_title; // Output: My WebsiteIn this example, we set two global variables called 'site_title' and 'menu_items' to the Twig environment. We then use the getGlobals() method to retrieve the 'site_title' variable and store it in a variable called $site_title. We then print the value of $site_title which will output 'My Website'. Package Library: Twig is part of the Symfony framework but it can also be used as a standalone package in any PHP project. Twig can be installed using composer by including the "twig/twig" package.