Skip to content

Commit 96717b7

Browse files
committed
Merge pull request #43 from richleland/twig-view-tweaks
Twig view tweaks
2 parents 75d1d44 + 5d14175 commit 96717b7

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Views/README.markdown

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,37 @@ library. You can use the Twig custom view in your Slim Framework application lik
1717
If you are not using Composer to autoload project dependencies, you must also set the Twig view's public static
1818
`$twigDirectory` property; this is the relative or absolute path to the directory that conatins the Twig library.
1919

20-
You may also set the public static `$twigOptions` property; this is an array of settings that customize the Twig
21-
library behavior.
20+
### Twig configuration
21+
22+
There are several public static properties you can use to customize the Twig library behavior.
23+
24+
####$twigOptions
25+
26+
An array of options to pass to the underlying Twig environment ([Twig docs](http://twig.sensiolabs.org/doc/api.html#environment-options)):
27+
28+
\Slim\Extras\Views\Twig::$twigOptions = array(
29+
'debug' => true
30+
);
31+
32+
33+
####$twigExtensions
34+
35+
An array contianing Twig extensions to load ([Twig docs](http://twig.sensiolabs.org/doc/advanced.html)):
36+
37+
\Slim\Extras\Views\Twig::$twigExtensions = array(
38+
new MyCustomExtension(),
39+
new ThirdPartyExtension()
40+
);
41+
42+
43+
####$twigTemplateDirs
44+
45+
An array of paths to directories containing Twig templates ([Twig docs](http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem)):
46+
47+
\Slim\Extras\Views\Twig::$twigTemplateDirs = array(
48+
realpath(PROJECT_DIR . '/templates'),
49+
realpath(PROJECT_DIR . '/some/other/templates')
50+
);
2251

2352
## Mustache
2453

Views/Twig.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Twig extends \Slim\View
4646
*/
4747
public static $twigDirectory = null;
4848

49+
/**
50+
* @var array Paths to directories to attempt to load Twig template from
51+
*/
52+
public static $twigTemplateDirs = array();
53+
4954
/**
5055
* @var array The options for the Twig environment, see
5156
* http://www.twig-project.org/book/03-Twig-for-Developers
@@ -62,6 +67,22 @@ class Twig extends \Slim\View
6267
*/
6368
private $twigEnvironment = null;
6469

70+
/**
71+
* Get a list of template directories
72+
*
73+
* Returns an array of templates defined by self::$twigTemplateDirs, falls
74+
* back to Slim\View's built-in getTemplatesDirectory method.
75+
*
76+
* @return array
77+
**/
78+
private function getTemplateDirs()
79+
{
80+
if (empty(self::$twigTemplateDirs)) {
81+
return array($this->getTemplatesDirectory());
82+
}
83+
return self::$twigTemplateDirs;
84+
}
85+
6586
/**
6687
* Render Twig Template
6788
*
@@ -92,7 +113,7 @@ public function getEnvironment()
92113
}
93114

94115
\Twig_Autoloader::register();
95-
$loader = new \Twig_Loader_Filesystem($this->getTemplatesDirectory());
116+
$loader = new \Twig_Loader_Filesystem($this->getTemplateDirs());
96117
$this->twigEnvironment = new \Twig_Environment(
97118
$loader,
98119
self::$twigOptions

0 commit comments

Comments
 (0)