$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``
......................
/**
* 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();
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();
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, '/\\'));
}
/**
$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)
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'));
+ }
}