Skip to content
This repository
Browse code

added namespaced templates support in Twig_Loader_Filesystem

  • Loading branch information...
commit 0eb4d01d925a9f4a77403b241c2b2d616a8ac17a 1 parent c24ea1b
Fabien Potencier authored July 14, 2012
3  CHANGELOG
... ...
@@ -1,5 +1,6 @@
1  
-* 1.9.2 (2012-XX-XX)
  1
+* 1.10.0 (2012-XX-XX)
2 2
 
  3
+ * added namespaced templates support in Twig_Loader_Filesystem
3 4
  * added Twig_Loader_Filesystem::prependPath()
4 5
 
5 6
 * 1.9.1 (2012-07-22)
2  composer.json
@@ -25,7 +25,7 @@
25 25
     },
26 26
     "extra": {
27 27
         "branch-alias": {
28  
-            "dev-master": "1.9-dev"
  28
+            "dev-master": "1.10-dev"
29 29
         }
30 30
     }
31 31
 }
13  doc/api.rst
Source Rendered
@@ -148,6 +148,19 @@ methods::
148 148
     $loader->addPath($templateDir3);
149 149
     $loader->prependPath($templateDir4);
150 150
 
  151
+The filesystem loader also supports namespaced templates. This allows to
  152
+divide your templates in different namespaces; each namespace having its own
  153
+template paths. Namespaced templates can be accessed via the special
  154
+``namespace_name#template_path`` notation::
  155
+
  156
+    $loader->addPath($templateDir, 'admin');
  157
+
  158
+    $twig->render('admin#index.html', array());
  159
+
  160
+The ``setPaths()``, ``addPath()``, and ``prependPath()`` methods all takes a
  161
+namespace as an optional second argument; when not specified, these methods
  162
+work on the "main" namespace.
  163
+
151 164
 ``Twig_Loader_String``
152 165
 ......................
153 166
 
2  ext/twig/php_twig.h
@@ -15,7 +15,7 @@
15 15
 #ifndef PHP_TWIG_H
16 16
 #define PHP_TWIG_H
17 17
 
18  
-#define PHP_TWIG_VERSION "1.9.2-DEV"
  18
+#define PHP_TWIG_VERSION "1.10.0-DEV"
19 19
 
20 20
 #include "php.h"
21 21
 
43  lib/Twig/Loader/Filesystem.php
@@ -33,36 +33,40 @@ public function __construct($paths)
33 33
     /**
34 34
      * Returns the paths to the templates.
35 35
      *
  36
+     * @param string $namespace A path namespace
  37
+     *
36 38
      * @return array The array of paths where to look for templates
37 39
      */
38  
-    public function getPaths()
  40
+    public function getPaths($namespace = '')
39 41
     {
40  
-        return $this->paths;
  42
+        return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
41 43
     }
42 44
 
43 45
     /**
44 46
      * Sets the paths where templates are stored.
45 47
      *
46  
-     * @param string|array $paths A path or an array of paths where to look for templates
  48
+     * @param string|array $paths     A path or an array of paths where to look for templates
  49
+     * @param string       $namespace A path namespace
47 50
      */
48  
-    public function setPaths($paths)
  51
+    public function setPaths($paths, $namespace = '')
49 52
     {
50 53
         if (!is_array($paths)) {
51 54
             $paths = array($paths);
52 55
         }
53 56
 
54  
-        $this->paths = array();
  57
+        $this->paths[$namespace] = array();
55 58
         foreach ($paths as $path) {
56  
-            $this->addPath($path);
  59
+            $this->addPath($path, $namespace);
57 60
         }
58 61
     }
59 62
 
60 63
     /**
61 64
      * Adds a path where templates are stored.
62 65
      *
63  
-     * @param string $path A path where to look for templates
  66
+     * @param string $path      A path where to look for templates
  67
+     * @param string $namespace A path name
64 68
      */
65  
-    public function addPath($path)
  69
+    public function addPath($path, $namespace = '')
66 70
     {
67 71
         // invalidate the cache
68 72
         $this->cache = array();
@@ -71,15 +75,16 @@ public function addPath($path)
71 75
             throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
72 76
         }
73 77
 
74  
-        $this->paths[] = rtrim($path, '/\\');
  78
+        $this->paths[$namespace][] = rtrim($path, '/\\');
75 79
     }
76 80
 
77 81
     /**
78 82
      * Prepends a path where templates are stored.
79 83
      *
80  
-     * @param string $path A path where to look for templates
  84
+     * @param string $path      A path where to look for templates
  85
+     * @param string $namespace A path name
81 86
      */
82  
-    public function prependPath($path)
  87
+    public function prependPath($path, $namespace = '')
83 88
     {
84 89
         // invalidate the cache
85 90
         $this->cache = array();
@@ -88,7 +93,7 @@ public function prependPath($path)
88 93
             throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
89 94
         }
90 95
 
91  
-        array_unshift($this->paths, rtrim($path, '/\\'));
  96
+        array_unshift($this->paths[$namespace], rtrim($path, '/\\'));
92 97
     }
93 98
 
94 99
     /**
@@ -137,13 +142,23 @@ protected function findTemplate($name)
137 142
 
138 143
         $this->validateName($name);
139 144
 
140  
-        foreach ($this->paths as $path) {
  145
+        $namespace = '';
  146
+        if (false !== $pos = strpos($name, '#')) {
  147
+            $namespace = substr($name, 0, $pos);
  148
+            $name = substr($name, $pos + 1);
  149
+        }
  150
+
  151
+        if (!isset($this->paths[$namespace])) {
  152
+            throw new \Twig_Error_Loader(sprintf('There is not registered paths for path name "%s".', $namespace));
  153
+        }
  154
+
  155
+        foreach ($this->paths[$namespace] as $path) {
141 156
             if (is_file($path.'/'.$name)) {
142 157
                 return $this->cache[$name] = $path.'/'.$name;
143 158
             }
144 159
         }
145 160
 
146  
-        throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths)));
  161
+        throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
147 162
     }
148 163
 
149 164
     protected function validateName($name)
28  test/Twig/Tests/Loader/FilesystemTest.php
@@ -49,4 +49,32 @@ public function getSecurityTests()
49 49
             array('filters\\//../\\/\\..\\AutoloaderTest.php'),
50 50
         );
51 51
     }
  52
+
  53
+    public function testPaths()
  54
+    {
  55
+        $basePath = dirname(__FILE__).'/Fixtures';
  56
+
  57
+        $loader = new Twig_Loader_Filesystem(array($basePath.'/normal', $basePath.'/normal_bis'));
  58
+        $loader->setPaths(array($basePath.'/named', $basePath.'/named_bis'), 'named');
  59
+        $loader->addPath($basePath.'/named_ter', 'named');
  60
+        $loader->addPath($basePath.'/normal_ter');
  61
+        $loader->prependPath($basePath.'/normal_final');
  62
+        $loader->prependPath($basePath.'/named_final', 'named');
  63
+
  64
+        $this->assertEquals(array(
  65
+            $basePath.'/normal_final',
  66
+            $basePath.'/normal',
  67
+            $basePath.'/normal_bis',
  68
+            $basePath.'/normal_ter',
  69
+        ), $loader->getPaths());
  70
+        $this->assertEquals(array(
  71
+            $basePath.'/named_final',
  72
+            $basePath.'/named',
  73
+            $basePath.'/named_bis',
  74
+            $basePath.'/named_ter',
  75
+        ), $loader->getPaths('named'));
  76
+
  77
+        $this->assertEquals("path (final)\n", $loader->getSource('index.html'));
  78
+        $this->assertEquals("named path (final)\n", $loader->getSource('named#index.html'));
  79
+    }
52 80
 }
1  test/Twig/Tests/Loader/Fixtures/named/index.html
... ...
@@ -0,0 +1 @@
  1
+named path
1  test/Twig/Tests/Loader/Fixtures/named_bis/index.html
... ...
@@ -0,0 +1 @@
  1
+named path (bis)
1  test/Twig/Tests/Loader/Fixtures/named_final/index.html
... ...
@@ -0,0 +1 @@
  1
+named path (final)
1  test/Twig/Tests/Loader/Fixtures/named_ter/index.html
... ...
@@ -0,0 +1 @@
  1
+named path (ter)
1  test/Twig/Tests/Loader/Fixtures/normal/index.html
... ...
@@ -0,0 +1 @@
  1
+path
1  test/Twig/Tests/Loader/Fixtures/normal_bis/index.html
... ...
@@ -0,0 +1 @@
  1
+path (bis)
1  test/Twig/Tests/Loader/Fixtures/normal_final/index.html
... ...
@@ -0,0 +1 @@
  1
+path (final)
1  test/Twig/Tests/Loader/Fixtures/normal_ter/index.html
... ...
@@ -0,0 +1 @@
  1
+path (ter)

0 notes on commit 0eb4d01

Please sign in to comment.
Something went wrong with that request. Please try again.