Skip to content

Commit 19c3fed

Browse files
committed
Add $this->twig->addGlobal()
1 parent a675f21 commit 19c3fed

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

libraries/Twig.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ class Twig
2727
'form_open', 'form_close', 'form_error', 'set_value', 'form_hidden'
2828
];
2929

30+
/**
31+
* @var Twig_Environment
32+
*/
3033
private $twig;
34+
35+
/**
36+
* @var Twig_Loader_Filesystem
37+
*/
3138
private $loader;
3239

3340
public function __construct($params = [])
@@ -83,11 +90,23 @@ public function setLoader($loader)
8390
$this->loader = $loader;
8491
}
8592

93+
/**
94+
* Registers a Global
95+
*
96+
* @param string $name The global name
97+
* @param mixed $value The global value
98+
*/
99+
public function addGlobal($name, $value)
100+
{
101+
$this->createTwig();
102+
$this->twig->addGlobal($name, $value);
103+
}
104+
86105
/**
87106
* Renders Twig Template and Set Output
88107
*
89-
* @param string $view template filename without `.twig`
90-
* @param array $params
108+
* @param string $view Template filename without `.twig`
109+
* @param array $params Array of parameters to pass to the template
91110
*/
92111
public function display($view, $params = [])
93112
{
@@ -98,8 +117,8 @@ public function display($view, $params = [])
98117
/**
99118
* Renders Twig Template and Returns as String
100119
*
101-
* @param string $view template filename without `.twig`
102-
* @param array $params
120+
* @param string $view Template filename without `.twig`
121+
* @param array $params Array of parameters to pass to the template
103122
* @return string
104123
*/
105124
public function render($view, $params = [])
@@ -157,7 +176,7 @@ private function addCIFunctions()
157176
/**
158177
* @param string $uri
159178
* @param string $title
160-
* @param array $attributes [changed] only array is acceptable
179+
* @param array $attributes [changed] only array is acceptable
161180
* @return string
162181
*/
163182
public function safe_anchor($uri = '', $title = '', $attributes = [])
@@ -179,6 +198,7 @@ public function safe_anchor($uri = '', $title = '', $attributes = [])
179198
*/
180199
public function getTwig()
181200
{
201+
$this->createTwig();
182202
return $this->twig;
183203
}
184204
}

tests/libraries/TwigTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public function testDisplay()
3434
$output = $CI->output->get_output();
3535
$this->assertEquals('Hello CodeIgniter!'."\n", $output);
3636
}
37+
38+
public function testAddGlobal()
39+
{
40+
$obj = new Twig(['paths' => __DIR__ . '/../templates/']);
41+
$obj->addGlobal('sitename', 'Twig Test Site');
42+
43+
$output = $obj->render('global');
44+
$this->assertEquals('<title>Twig Test Site</title>'."\n", $output);
45+
}
3746
}

tests/templates/global.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<title>{{ sitename }}</title>

0 commit comments

Comments
 (0)