Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added namespaced templates support in Twig_Loader_Filesystem

  • Loading branch information...
commit 0eb4d01d925a9f4a77403b241c2b2d616a8ac17a 1 parent c24ea1b
@fabpot authored
View
3  CHANGELOG
@@ -1,5 +1,6 @@
-* 1.9.2 (2012-XX-XX)
+* 1.10.0 (2012-XX-XX)
+ * added namespaced templates support in Twig_Loader_Filesystem
* added Twig_Loader_Filesystem::prependPath()
* 1.9.1 (2012-07-22)
View
2  composer.json
@@ -25,7 +25,7 @@
},
"extra": {
"branch-alias": {
- "dev-master": "1.9-dev"
+ "dev-master": "1.10-dev"
}
}
}
View
13 doc/api.rst
@@ -148,6 +148,19 @@ methods::
$loader->addPath($templateDir3);
$loader->prependPath($templateDir4);
+The filesystem loader also supports namespaced templates. This allows to
+divide your templates in different namespaces; each namespace having its own
+template paths. Namespaced templates can be accessed via the special
+``namespace_name#template_path`` notation::
+
+ $loader->addPath($templateDir, 'admin');
+
+ $twig->render('admin#index.html', array());
+
+The ``setPaths()``, ``addPath()``, and ``prependPath()`` methods all takes a
+namespace as an optional second argument; when not specified, these methods
+work on the "main" namespace.
+
``Twig_Loader_String``
......................
View
2  ext/twig/php_twig.h
@@ -15,7 +15,7 @@
#ifndef PHP_TWIG_H
#define PHP_TWIG_H
-#define PHP_TWIG_VERSION "1.9.2-DEV"
+#define PHP_TWIG_VERSION "1.10.0-DEV"
#include "php.h"
View
43 lib/Twig/Loader/Filesystem.php
@@ -33,36 +33,40 @@ public function __construct($paths)
/**
* Returns the paths to the templates.
*
+ * @param string $namespace A path namespace
+ *
* @return array The array of paths where to look for templates
*/
- public function getPaths()
+ public function getPaths($namespace = '')
{
- return $this->paths;
+ return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
}
/**
* Sets the paths where templates are stored.
*
- * @param string|array $paths A path or an array of paths where to look for templates
+ * @param string|array $paths A path or an array of paths where to look for templates
+ * @param string $namespace A path namespace
*/
- public function setPaths($paths)
+ public function setPaths($paths, $namespace = '')
{
if (!is_array($paths)) {
$paths = array($paths);
}
- $this->paths = array();
+ $this->paths[$namespace] = array();
foreach ($paths as $path) {
- $this->addPath($path);
+ $this->addPath($path, $namespace);
}
}
/**
* Adds a path where templates are stored.
*
- * @param string $path A path where to look for templates
+ * @param string $path A path where to look for templates
+ * @param string $namespace A path name
*/
- public function addPath($path)
+ public function addPath($path, $namespace = '')
{
// invalidate the cache
$this->cache = array();
@@ -71,15 +75,16 @@ public function addPath($path)
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
}
- $this->paths[] = rtrim($path, '/\\');
+ $this->paths[$namespace][] = rtrim($path, '/\\');
}
/**
* Prepends a path where templates are stored.
*
- * @param string $path A path where to look for templates
+ * @param string $path A path where to look for templates
+ * @param string $namespace A path name
*/
- public function prependPath($path)
+ public function prependPath($path, $namespace = '')
{
// invalidate the cache
$this->cache = array();
@@ -88,7 +93,7 @@ public function prependPath($path)
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
}
- array_unshift($this->paths, rtrim($path, '/\\'));
+ array_unshift($this->paths[$namespace], rtrim($path, '/\\'));
}
/**
@@ -137,13 +142,23 @@ protected function findTemplate($name)
$this->validateName($name);
- foreach ($this->paths as $path) {
+ $namespace = '';
+ if (false !== $pos = strpos($name, '#')) {
+ $namespace = substr($name, 0, $pos);
+ $name = substr($name, $pos + 1);
+ }
+
+ if (!isset($this->paths[$namespace])) {
+ throw new \Twig_Error_Loader(sprintf('There is not registered paths for path name "%s".', $namespace));
+ }
+
+ foreach ($this->paths[$namespace] as $path) {
if (is_file($path.'/'.$name)) {
return $this->cache[$name] = $path.'/'.$name;
}
}
- throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths)));
+ throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
}
protected function validateName($name)
View
28 test/Twig/Tests/Loader/FilesystemTest.php
@@ -49,4 +49,32 @@ public function getSecurityTests()
array('filters\\//../\\/\\..\\AutoloaderTest.php'),
);
}
+
+ public function testPaths()
+ {
+ $basePath = dirname(__FILE__).'/Fixtures';
+
+ $loader = new Twig_Loader_Filesystem(array($basePath.'/normal', $basePath.'/normal_bis'));
+ $loader->setPaths(array($basePath.'/named', $basePath.'/named_bis'), 'named');
+ $loader->addPath($basePath.'/named_ter', 'named');
+ $loader->addPath($basePath.'/normal_ter');
+ $loader->prependPath($basePath.'/normal_final');
+ $loader->prependPath($basePath.'/named_final', 'named');
+
+ $this->assertEquals(array(
+ $basePath.'/normal_final',
+ $basePath.'/normal',
+ $basePath.'/normal_bis',
+ $basePath.'/normal_ter',
+ ), $loader->getPaths());
+ $this->assertEquals(array(
+ $basePath.'/named_final',
+ $basePath.'/named',
+ $basePath.'/named_bis',
+ $basePath.'/named_ter',
+ ), $loader->getPaths('named'));
+
+ $this->assertEquals("path (final)\n", $loader->getSource('index.html'));
+ $this->assertEquals("named path (final)\n", $loader->getSource('named#index.html'));
+ }
}
View
1  test/Twig/Tests/Loader/Fixtures/named/index.html
@@ -0,0 +1 @@
+named path
View
1  test/Twig/Tests/Loader/Fixtures/named_bis/index.html
@@ -0,0 +1 @@
+named path (bis)
View
1  test/Twig/Tests/Loader/Fixtures/named_final/index.html
@@ -0,0 +1 @@
+named path (final)
View
1  test/Twig/Tests/Loader/Fixtures/named_ter/index.html
@@ -0,0 +1 @@
+named path (ter)
View
1  test/Twig/Tests/Loader/Fixtures/normal/index.html
@@ -0,0 +1 @@
+path
View
1  test/Twig/Tests/Loader/Fixtures/normal_bis/index.html
@@ -0,0 +1 @@
+path (bis)
View
1  test/Twig/Tests/Loader/Fixtures/normal_final/index.html
@@ -0,0 +1 @@
+path (final)
View
1  test/Twig/Tests/Loader/Fixtures/normal_ter/index.html
@@ -0,0 +1 @@
+path (ter)
Please sign in to comment.
Something went wrong with that request. Please try again.