Skip to content

Commit 6bc91f3

Browse files
author
Davide Bellini
committed
Implemented the Twig addGloba() method for views global data
It can be useful to get a variable accessible in all views using a global context (e.g. Twig Macros)
1 parent 6aba6b2 commit 6bc91f3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

classes/view/twig.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public static function _init()
3333
protected function process_file($file_override = false)
3434
{
3535
$file = $file_override ?: $this->file_name;
36-
$data = $this->get_data();
36+
37+
$local_data = $this->get_data('local');
38+
$global_data = $this->get_data('global');
3739

3840
// Extract View name/extension (ex. "template.twig")
3941
$view_name = pathinfo($file, PATHINFO_BASENAME);
@@ -43,9 +45,17 @@ protected function process_file($file_override = false)
4345
array_unshift($views_paths, pathinfo($file, PATHINFO_DIRNAME));
4446
static::$_parser_loader = new Twig_Loader_Filesystem($views_paths);
4547

48+
if ( ! empty($global_data))
49+
{
50+
foreach ($global_data as $key => $value)
51+
{
52+
static::parser()->addGlobal($key, $value);
53+
}
54+
}
55+
4656
try
4757
{
48-
return static::parser()->loadTemplate($view_name)->render($data);
58+
return static::parser()->loadTemplate($view_name)->render($local_data);
4959
}
5060
catch (\Exception $e)
5161
{

0 commit comments

Comments
 (0)