1414
1515use Exception ;
1616use Twig \Environment ;
17+ use Twig \Error \LoaderError ;
1718use Twig \Extension \DebugExtension ;
1819use Twig \Loader \FilesystemLoader ;
1920use Twig \TwigFilter ;
@@ -25,6 +26,11 @@ class Template {
2526 */
2627 private array $ globals = [];
2728
29+ /**
30+ * @var array<string, string>
31+ */
32+ private array $ paths = [];
33+
2834 /**
2935 * Add global template variable.
3036 *
@@ -37,6 +43,18 @@ public function addGlobal(string $name, $value): void {
3743 $ this ->globals [$ name ] = $ value ;
3844 }
3945
46+ /**
47+ * Add a path with namespace.
48+ *
49+ * @param string $namespace
50+ * @param string $path
51+ *
52+ * @return void
53+ */
54+ public function addPath (string $ namespace , string $ path ): void {
55+ $ this ->paths [$ namespace ] = $ path ;
56+ }
57+
4058 /**
4159 * Render template.
4260 *
@@ -53,6 +71,14 @@ public function render(string $tpl, array $data = []): string {
5371 'debug ' => Config::get ('twigdebug ' ),
5472 ]);
5573
74+ foreach ($ this ->paths as $ namespace => $ path ) {
75+ try {
76+ $ loader ->addPath (realpath ($ path ), $ namespace );
77+ } catch (LoaderError $ e ) {
78+ echo $ e ->getMessage ();
79+ }
80+ }
81+
5682 if (Config::get ('twigdebug ' )) {
5783 $ twig ->addExtension (new DebugExtension ());
5884 }
0 commit comments