mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
added namespaced templates support in Twig_Loader_Filesystem
This commit is contained in:
@@ -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()
|
* added Twig_Loader_Filesystem::prependPath()
|
||||||
|
|
||||||
* 1.9.1 (2012-07-22)
|
* 1.9.1 (2012-07-22)
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@
|
|||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "1.9-dev"
|
"dev-master": "1.10-dev"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
@@ -148,6 +148,19 @@ methods::
|
|||||||
$loader->addPath($templateDir3);
|
$loader->addPath($templateDir3);
|
||||||
$loader->prependPath($templateDir4);
|
$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``
|
``Twig_Loader_String``
|
||||||
......................
|
......................
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@
|
|||||||
#ifndef PHP_TWIG_H
|
#ifndef PHP_TWIG_H
|
||||||
#define 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"
|
#include "php.h"
|
||||||
|
|
||||||
|
|||||||
@@ -33,36 +33,40 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
/**
|
/**
|
||||||
* Returns the paths to the templates.
|
* Returns the paths to the templates.
|
||||||
*
|
*
|
||||||
|
* @param string $namespace A path namespace
|
||||||
|
*
|
||||||
* @return array The array of paths where to look for templates
|
* @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.
|
* 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)) {
|
if (!is_array($paths)) {
|
||||||
$paths = array($paths);
|
$paths = array($paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->paths = array();
|
$this->paths[$namespace] = array();
|
||||||
foreach ($paths as $path) {
|
foreach ($paths as $path) {
|
||||||
$this->addPath($path);
|
$this->addPath($path, $namespace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a path where templates are stored.
|
* 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
|
// invalidate the cache
|
||||||
$this->cache = array();
|
$this->cache = array();
|
||||||
@@ -71,15 +75,16 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $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.
|
* 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
|
// invalidate the cache
|
||||||
$this->cache = array();
|
$this->cache = array();
|
||||||
@@ -88,7 +93,7 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $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 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
|||||||
|
|
||||||
$this->validateName($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)) {
|
if (is_file($path.'/'.$name)) {
|
||||||
return $this->cache[$name] = $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)
|
protected function validateName($name)
|
||||||
|
|||||||
@@ -49,4 +49,32 @@ class Twig_Tests_Loader_FilesystemTest extends PHPUnit_Framework_TestCase
|
|||||||
array('filters\\//../\\/\\..\\AutoloaderTest.php'),
|
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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
named path
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
named path (bis)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
named path (final)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
named path (ter)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
path
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
path (bis)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
path (final)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
path (ter)
|
||||||
Reference in New Issue
Block a user