From aea48b9b58282a15fb470c1ae15b3f418a988256 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Mon, 17 Sep 2018 10:34:07 +0100 Subject: [PATCH 1/9] Add support for path namespaces. Particularly useful if using a styleguide which uses a particular namespace. --- lib/Loader.php | 9 ++++++++- lib/LocationManager.php | 10 +++++++--- tests/test-timber-loader.php | 5 +++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/Loader.php b/lib/Loader.php index 30455fdb5..0886b81da 100644 --- a/lib/Loader.php +++ b/lib/Loader.php @@ -138,7 +138,14 @@ public function get_loader() { if ( $open_basedir ) { $rootPath = null; } - $fs = new \Twig_Loader_Filesystem($paths, $rootPath); + $fs = new \Twig_Loader_Filesystem(array(), $rootPath); + foreach ($paths as $namespace => $path) { + if (is_string($namespace)) { + $fs->addPath($path, $namespace); + } else { + $fs->addPath($path); + } + } $fs = apply_filters('timber/loader/loader', $fs); return $fs; } diff --git a/lib/LocationManager.php b/lib/LocationManager.php index 62ffbe488..c9825bed0 100644 --- a/lib/LocationManager.php +++ b/lib/LocationManager.php @@ -109,10 +109,14 @@ protected static function get_locations_user() { if ( is_string(Timber::$locations) ) { Timber::$locations = array(Timber::$locations); } - foreach ( Timber::$locations as $tloc ) { + foreach ( Timber::$locations as $namespace => $tloc ) { $tloc = realpath($tloc); if ( is_dir($tloc) ) { - $locs[] = $tloc; + if ( ! is_string( $namespace ) ) { + $locs[] = $tloc; + } else { + $locs[$namespace] = $tloc; + } } } } @@ -142,4 +146,4 @@ protected static function get_locations_caller( $caller = false ) { } -} \ No newline at end of file +} diff --git a/tests/test-timber-loader.php b/tests/test-timber-loader.php index 0535399c0..648082142 100644 --- a/tests/test-timber-loader.php +++ b/tests/test-timber-loader.php @@ -124,5 +124,10 @@ function testTwigLoadsFromLocation(){ $this->assertEquals('', trim($str)); } + function testTwigLoadsFromLocationWithNamespace(){ + Timber::$locations = array( 'assets' => __DIR__.'/assets' ); + $str = Timber::compile('@assets/thumb-test.twig'); + $this->assertEquals('', trim($str)); + } } From 726a2d2695f35c0bbcc71a6827d2987ed0dabf5e Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Wed, 19 Sep 2018 15:33:28 +0100 Subject: [PATCH 2/9] Add more tests to avoid regression --- bin/install-wp-tests.sh | 0 tests/namespaced/test-namespaced.twig | 1 + tests/test-timber-loader.php | 34 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) mode change 100644 => 100755 bin/install-wp-tests.sh create mode 100644 tests/namespaced/test-namespaced.twig diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh old mode 100644 new mode 100755 diff --git a/tests/namespaced/test-namespaced.twig b/tests/namespaced/test-namespaced.twig new file mode 100644 index 000000000..79e526307 --- /dev/null +++ b/tests/namespaced/test-namespaced.twig @@ -0,0 +1 @@ +This is a namespaced template. diff --git a/tests/test-timber-loader.php b/tests/test-timber-loader.php index 648082142..812f0340c 100644 --- a/tests/test-timber-loader.php +++ b/tests/test-timber-loader.php @@ -130,4 +130,38 @@ function testTwigLoadsFromLocationWithNamespace(){ $this->assertEquals('', trim($str)); } + function testTwigLoadsFromLocationWithAndWithoutNamespaces(){ + Timber::$locations = array( 'namespaced' => __DIR__.'/namespaced', __DIR__ . '/assets' ); + + // Namespaced location + $str = Timber::compile('@namespaced/test-namespaced.twig'); + $this->assertEquals('This is a namespaced template.', trim($str)); + + // Non namespaced location + $str = Timber::compile('thumb-test.twig'); + $this->assertEquals('', trim($str)); + } + + function testTwigLoadsFromLocationWithAndWithoutNamespacesAndDirs(){ + Timber::$dirname = array('foo', 'views'); + Timber::$locations = array( 'namespaced' => __DIR__.'/namespaced', __DIR__ . '/assets' ); + + // Namespaced location + $str = Timber::compile('@namespaced/test-namespaced.twig'); + $this->assertEquals('This is a namespaced template.', trim($str)); + + // Non namespaced location + $str = Timber::compile('thumb-test.twig'); + $this->assertEquals('', trim($str)); + + if (!file_exists(get_template_directory().'/foo')) { + mkdir(get_template_directory().'/foo', 0777, true); + } + copy(__DIR__.'/assets/single-foo.twig', get_template_directory().'/foo/single-foo.twig'); + + // Dir + $str = Timber::compile('single-foo.twig'); + $this->assertEquals('I am single-foo', trim($str)); + } + } From 06f795ef495585a799402da87ee0e701f2a91466 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Thu, 20 Sep 2018 11:00:21 +0100 Subject: [PATCH 3/9] Add support for multiple paths per namespace --- lib/Loader.php | 25 +++++---- lib/LocationManager.php | 89 +++++++++++++++++++------------ lib/Timber.php | 28 ++++++---- tests/namespaced/test-nested.twig | 1 + tests/namespaced/thumb-test.twig | 1 + tests/test-timber-loader.php | 28 ++++++++-- 6 files changed, 117 insertions(+), 55 deletions(-) create mode 100644 tests/namespaced/test-nested.twig create mode 100644 tests/namespaced/thumb-test.twig diff --git a/lib/Loader.php b/lib/Loader.php index 0886b81da..5c6cce2c1 100644 --- a/lib/Loader.php +++ b/lib/Loader.php @@ -3,6 +3,7 @@ namespace Timber; use Timber\Cache\Cleaner; +use Twig\Loader\FilesystemLoader; class Loader { @@ -130,23 +131,29 @@ protected function template_exists( $name ) { * @return \Twig_Loader_Filesystem */ public function get_loader() { - $open_basedir = ini_get('open_basedir'); - $paths = array_merge($this->locations, array($open_basedir ? ABSPATH : '/')); - $paths = apply_filters('timber/loader/paths', $paths); + $open_basedir = ini_get( 'open_basedir' ); + $paths = array_merge_recursive( + $this->locations, + array( FilesystemLoader::MAIN_NAMESPACE => $open_basedir ? ABSPATH : '/' ) + ); + $paths = apply_filters( 'timber/loader/paths', $paths ); $rootPath = '/'; if ( $open_basedir ) { $rootPath = null; } - $fs = new \Twig_Loader_Filesystem(array(), $rootPath); - foreach ($paths as $namespace => $path) { - if (is_string($namespace)) { - $fs->addPath($path, $namespace); + $fs = new \Twig_Loader_Filesystem( array(), $rootPath ); + foreach ( $paths as $namespace => $path_locations ) { + if ( is_array( $path_locations ) ) { + array_map( function ( $path ) use ( $fs, $namespace ) { + $fs->addPath( $path, $namespace ); + }, $path_locations ); } else { - $fs->addPath($path); + $fs->addPath( $path_locations, $namespace ); } } - $fs = apply_filters('timber/loader/loader', $fs); + $fs = apply_filters( 'timber/loader/loader', $fs ); + return $fs; } diff --git a/lib/LocationManager.php b/lib/LocationManager.php index c9825bed0..bf0da6277 100644 --- a/lib/LocationManager.php +++ b/lib/LocationManager.php @@ -2,6 +2,8 @@ namespace Timber; +use Twig\Loader\FilesystemLoader; + class LocationManager { @@ -12,17 +14,27 @@ class LocationManager { public static function get_locations( $caller = false ) { //prioirty: user locations, caller (but not theme), child theme, parent theme, caller $locs = array(); - $locs = array_merge($locs, self::get_locations_user()); - $locs = array_merge($locs, self::get_locations_caller($caller)); + $locs = array_merge_recursive( $locs, self::get_locations_user() ); + $locs = array_merge_recursive( $locs, self::get_locations_caller( $caller ) ); //remove themes from caller - $locs = array_diff($locs, self::get_locations_theme()); - $locs = array_merge($locs, self::get_locations_theme()); - $locs = array_merge($locs, self::get_locations_caller($caller)); - $locs = array_unique($locs); + //$locs = array_diff_assoc( $locs, self::get_locations_theme() ); + $locs = array_merge_recursive( $locs, self::get_locations_theme() ); + $locs = array_merge_recursive( $locs, self::get_locations_caller( $caller ) ); + $locs = array_map( 'array_unique', $locs ); + //now make sure theres a trailing slash on everything - $locs = array_map('trailingslashit', $locs); - $locs = apply_filters('timber_locations', $locs); - $locs = apply_filters('timber/locations', $locs); + $locs = array_map( function ( $loc ) { + return array_map( 'trailingslashit', $loc ); + }, $locs ); + + $locs = array_map( function ( $loc ) { + return apply_filters( 'timber_locations', $loc ); + }, $locs ); + + $locs = array_map( function ( $loc ) { + return apply_filters( 'timber/locations', $loc ); + }, $locs ); + return $locs; } @@ -33,19 +45,19 @@ public static function get_locations( $caller = false ) { protected static function get_locations_theme() { $theme_locs = array(); $theme_dirs = LocationManager::get_locations_theme_dir(); - $roots = array(get_stylesheet_directory(), get_template_directory()); - $roots = array_map('realpath', $roots); - $roots = array_unique($roots); + $roots = array( get_stylesheet_directory(), get_template_directory() ); + $roots = array_map( 'realpath', $roots ); + $roots = array_unique( $roots ); foreach ( $roots as $root ) { - if ( !is_dir($root) ) { + if ( ! is_dir( $root ) ) { continue; } - $theme_locs[] = $root; - $root = trailingslashit($root); + $theme_locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $root; + $root = trailingslashit( $root ); foreach ( $theme_dirs as $dirname ) { - $tloc = realpath($root.$dirname); - if ( is_dir($tloc) ) { - $theme_locs[] = $tloc; + $tloc = realpath( $root . $dirname ); + if ( is_dir( $tloc ) ) { + $theme_locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $tloc; } } } @@ -105,21 +117,29 @@ public static function get_locations_theme_dir() { */ protected static function get_locations_user() { $locs = array(); - if ( isset(Timber::$locations) ) { - if ( is_string(Timber::$locations) ) { - Timber::$locations = array(Timber::$locations); + if ( isset( Timber::$locations ) ) { + if ( is_string( Timber::$locations ) ) { + Timber::$locations = array( Timber::$locations ); } - foreach ( Timber::$locations as $namespace => $tloc ) { - $tloc = realpath($tloc); - if ( is_dir($tloc) ) { + foreach ( Timber::$locations as $tloc => $namespace_or_tloc ) { + if ( is_string( $tloc ) ) { + $namespace = $namespace_or_tloc; + } else { + $tloc = $namespace_or_tloc; + $namespace = null; + } + + $tloc = realpath( $tloc ); + if ( is_dir( $tloc ) ) { if ( ! is_string( $namespace ) ) { - $locs[] = $tloc; + $locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $tloc; } else { - $locs[$namespace] = $tloc; + $locs[ $namespace ][] = $tloc; } } } } + return $locs; } @@ -129,19 +149,20 @@ protected static function get_locations_user() { */ protected static function get_locations_caller( $caller = false ) { $locs = array(); - if ( $caller && is_string($caller) ) { - $caller = realpath($caller); - if ( is_dir($caller) ) { - $locs[] = $caller; + if ( $caller && is_string( $caller ) ) { + $caller = realpath( $caller ); + if ( is_dir( $caller ) ) { + $locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $caller; } - $caller = trailingslashit($caller); + $caller = trailingslashit( $caller ); foreach ( LocationManager::get_locations_theme_dir() as $dirname ) { - $caller_sub = realpath($caller.$dirname); - if ( is_dir($caller_sub) ) { - $locs[] = $caller_sub; + $caller_sub = realpath( $caller . $dirname ); + if ( is_dir( $caller_sub ) ) { + $locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $caller_sub; } } } + return $locs; } diff --git a/lib/Timber.php b/lib/Timber.php index b106c8a26..4100b4118 100644 --- a/lib/Timber.php +++ b/lib/Timber.php @@ -439,22 +439,32 @@ public static function get_sidebar( $sidebar = 'sidebar.php', $data = array() ) * @return string */ public static function get_sidebar_from_php( $sidebar = '', $data ) { - $caller = LocationManager::get_calling_script_dir(1); - $uris = LocationManager::get_locations($caller); + $caller = LocationManager::get_calling_script_dir( 1 ); + $uris = LocationManager::get_locations( $caller ); ob_start(); $found = false; - foreach ( $uris as $uri ) { - if ( file_exists(trailingslashit($uri).$sidebar) ) { - include trailingslashit($uri).$sidebar; - $found = true; - break; + foreach ( $uris as $namespace => $uri_locations ) { + if ( is_array( $uri_locations ) ) { + foreach ( $uri_locations as $uri ) { + if ( file_exists( trailingslashit( $uri ) . $sidebar ) ) { + include trailingslashit( $uri ) . $sidebar; + $found = true; + } + } + } else { + if ( file_exists( trailingslashit( $uri_locations ) . $sidebar ) ) { + include trailingslashit( $uri_locations ) . $sidebar; + $found = true; + break; + } } } - if ( !$found ) { - Helper::error_log('error loading your sidebar, check to make sure the file exists'); + if ( ! $found ) { + Helper::error_log( 'error loading your sidebar, check to make sure the file exists' ); } $ret = ob_get_contents(); ob_end_clean(); + return $ret; } diff --git a/tests/namespaced/test-nested.twig b/tests/namespaced/test-nested.twig new file mode 100644 index 000000000..ae71e17bd --- /dev/null +++ b/tests/namespaced/test-nested.twig @@ -0,0 +1 @@ +{% include '@namespaced/test-namespaced.twig' %} diff --git a/tests/namespaced/thumb-test.twig b/tests/namespaced/thumb-test.twig new file mode 100644 index 000000000..ce129953d --- /dev/null +++ b/tests/namespaced/thumb-test.twig @@ -0,0 +1 @@ +namespaced diff --git a/tests/test-timber-loader.php b/tests/test-timber-loader.php index 812f0340c..3ff65863f 100644 --- a/tests/test-timber-loader.php +++ b/tests/test-timber-loader.php @@ -24,6 +24,7 @@ function testBogusTemplates() { function testTwigPathFilter() { $php_unit = $this; add_filter('timber/loader/paths', function($paths) use ($php_unit) { + $paths = call_user_func_array('array_merge', $paths); $count = count($paths); $php_unit->assertEquals(3, count($paths)); $pos = array_search('/', $paths); @@ -125,13 +126,19 @@ function testTwigLoadsFromLocation(){ } function testTwigLoadsFromLocationWithNamespace(){ - Timber::$locations = array( 'assets' => __DIR__.'/assets' ); + Timber::$locations = array( __DIR__.'/assets' => 'assets' ); $str = Timber::compile('@assets/thumb-test.twig'); $this->assertEquals('', trim($str)); } + function testTwigLoadsFromLocationWithNestedNamespace(){ + Timber::$locations = array( __DIR__.'/namespaced' => 'namespaced' ); + $str = Timber::compile('@namespaced/test-nested.twig'); + $this->assertEquals('This is a namespaced template.', trim($str)); + } + function testTwigLoadsFromLocationWithAndWithoutNamespaces(){ - Timber::$locations = array( 'namespaced' => __DIR__.'/namespaced', __DIR__ . '/assets' ); + Timber::$locations = array( __DIR__.'/namespaced' => 'namespaced', __DIR__ . '/assets' ); // Namespaced location $str = Timber::compile('@namespaced/test-namespaced.twig'); @@ -144,7 +151,7 @@ function testTwigLoadsFromLocationWithAndWithoutNamespaces(){ function testTwigLoadsFromLocationWithAndWithoutNamespacesAndDirs(){ Timber::$dirname = array('foo', 'views'); - Timber::$locations = array( 'namespaced' => __DIR__.'/namespaced', __DIR__ . '/assets' ); + Timber::$locations = array( __DIR__.'/namespaced' => 'namespaced', __DIR__ . '/assets' ); // Namespaced location $str = Timber::compile('@namespaced/test-namespaced.twig'); @@ -164,4 +171,19 @@ function testTwigLoadsFromLocationWithAndWithoutNamespacesAndDirs(){ $this->assertEquals('I am single-foo', trim($str)); } + function testTwigLoadsFromMultipleLocationsWithNamespace(){ + Timber::$locations = array( __DIR__.'/assets' => 'assets', __DIR__ .'/namespaced' => 'assets' ); + $str = Timber::compile('@assets/thumb-test.twig'); + $this->assertEquals('', trim($str)); + + $str = Timber::compile('@assets/test-namespaced.twig'); + $this->assertEquals('This is a namespaced template.', trim($str)); + } + + function testTwigLoadsFirstTemplateWhenMultipleLocationsWithSameNamespace(){ + Timber::$locations = array( __DIR__.'/assets' => 'assets', __DIR__ .'/namespaced' => 'assets' ); + $str = Timber::compile('@assets/thumb-test.twig'); + $this->assertEquals('', trim($str)); + } + } From 46bbd7a3759bb7bd954b66378d1232cb433a1f56 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Thu, 20 Sep 2018 11:00:27 +0100 Subject: [PATCH 4/9] Add docs --- docs/guides/template-locations.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/guides/template-locations.md b/docs/guides/template-locations.md index 187712e56..aa314d4b1 100644 --- a/docs/guides/template-locations.md +++ b/docs/guides/template-locations.md @@ -23,6 +23,31 @@ Timber::$locations = array( ); ``` +You can also use namespaces in your locations too, just define it as the value next to a path, for example: + +```php + 'styleguide' +); +``` + +You can also register multiple paths for the same namespace. Order is important as it will look top to bottom and return the first one it encounters, for example: + +```php + 'styleguide', + '/Users/jared/Sandbox/styleguide' => 'styleguide' +); +``` + You only need to do this once in your project (in `functions.php` of your theme). When you call one of the render or compile functions from a PHP file (say `single.php`), Timber will look for Twig files in these locations before it checks the child or parent theme. ## Changing the default folder for Twig files From da5f17ebbc9cd5d478e873c7e2a43229d2e1ce7b Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Thu, 20 Sep 2018 15:07:52 +0100 Subject: [PATCH 5/9] Remove superfluous elses affecting code coverage --- lib/Loader.php | 2 -- lib/Timber.php | 6 ------ 2 files changed, 8 deletions(-) diff --git a/lib/Loader.php b/lib/Loader.php index 5c6cce2c1..94ed60762 100644 --- a/lib/Loader.php +++ b/lib/Loader.php @@ -148,8 +148,6 @@ public function get_loader() { array_map( function ( $path ) use ( $fs, $namespace ) { $fs->addPath( $path, $namespace ); }, $path_locations ); - } else { - $fs->addPath( $path_locations, $namespace ); } } $fs = apply_filters( 'timber/loader/loader', $fs ); diff --git a/lib/Timber.php b/lib/Timber.php index 4100b4118..ed123aadc 100644 --- a/lib/Timber.php +++ b/lib/Timber.php @@ -451,12 +451,6 @@ public static function get_sidebar_from_php( $sidebar = '', $data ) { $found = true; } } - } else { - if ( file_exists( trailingslashit( $uri_locations ) . $sidebar ) ) { - include trailingslashit( $uri_locations ) . $sidebar; - $found = true; - break; - } } } if ( ! $found ) { From f31adba1691712e44102f46611a2b523b2ae04c8 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Fri, 21 Sep 2018 14:06:36 +0100 Subject: [PATCH 6/9] Remove dependency on Twig\Loader\FilesystemLoader::MAIN_NAMESPACE. As custom loaders may be used, limit the dependency on FilesystemLoader in the LocationManager by defining our own main namespace. --- lib/Loader.php | 5 ++++- lib/LocationManager.php | 12 +++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/Loader.php b/lib/Loader.php index 94ed60762..cb05bf6cb 100644 --- a/lib/Loader.php +++ b/lib/Loader.php @@ -17,6 +17,9 @@ class Loader { const CACHE_SITE_TRANSIENT = 'site-transient'; const CACHE_USE_DEFAULT = 'default'; + /** Identifier of the main namespace. Will likely mirror Twig\Loader\FilesystemLoader::MAIN_NAMESPACE */ + const MAIN_NAMESPACE = '__main__'; + public static $cache_modes = array( self::CACHE_NONE, self::CACHE_OBJECT, @@ -134,7 +137,7 @@ public function get_loader() { $open_basedir = ini_get( 'open_basedir' ); $paths = array_merge_recursive( $this->locations, - array( FilesystemLoader::MAIN_NAMESPACE => $open_basedir ? ABSPATH : '/' ) + array( self::MAIN_NAMESPACE => $open_basedir ? ABSPATH : '/' ) ); $paths = apply_filters( 'timber/loader/paths', $paths ); diff --git a/lib/LocationManager.php b/lib/LocationManager.php index bf0da6277..eaae3cc4c 100644 --- a/lib/LocationManager.php +++ b/lib/LocationManager.php @@ -2,8 +2,6 @@ namespace Timber; -use Twig\Loader\FilesystemLoader; - class LocationManager { @@ -52,12 +50,12 @@ protected static function get_locations_theme() { if ( ! is_dir( $root ) ) { continue; } - $theme_locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $root; + $theme_locs[ Loader::MAIN_NAMESPACE ][] = $root; $root = trailingslashit( $root ); foreach ( $theme_dirs as $dirname ) { $tloc = realpath( $root . $dirname ); if ( is_dir( $tloc ) ) { - $theme_locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $tloc; + $theme_locs[ Loader::MAIN_NAMESPACE ][] = $tloc; } } } @@ -132,7 +130,7 @@ protected static function get_locations_user() { $tloc = realpath( $tloc ); if ( is_dir( $tloc ) ) { if ( ! is_string( $namespace ) ) { - $locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $tloc; + $locs[ Loader::MAIN_NAMESPACE ][] = $tloc; } else { $locs[ $namespace ][] = $tloc; } @@ -152,13 +150,13 @@ protected static function get_locations_caller( $caller = false ) { if ( $caller && is_string( $caller ) ) { $caller = realpath( $caller ); if ( is_dir( $caller ) ) { - $locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $caller; + $locs[ Loader::MAIN_NAMESPACE ][] = $caller; } $caller = trailingslashit( $caller ); foreach ( LocationManager::get_locations_theme_dir() as $dirname ) { $caller_sub = realpath( $caller . $dirname ); if ( is_dir( $caller_sub ) ) { - $locs[ FilesystemLoader::MAIN_NAMESPACE ][] = $caller_sub; + $locs[ Loader::MAIN_NAMESPACE ][] = $caller_sub; } } } From 9abb875f985827b741fc50006408848253c02c06 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Mon, 24 Sep 2018 08:27:47 +0100 Subject: [PATCH 7/9] Add an example of using a namespace within twig to docs --- docs/guides/template-locations.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/guides/template-locations.md b/docs/guides/template-locations.md index aa314d4b1..6e6ac7569 100644 --- a/docs/guides/template-locations.md +++ b/docs/guides/template-locations.md @@ -35,6 +35,15 @@ Timber::$locations = array( ); ``` +In the example above the namespace is called `styleguide`. You must prefix this with `@` when using it in templates (that's how Twig can differentiate namespaces from regular paths). +Assuming you have a template called `menu.twig` within that namespace, you would call it like so: + +```twig +{% include '@styleguide/menu.twig' %} +``` + +## Register your own namespaces + You can also register multiple paths for the same namespace. Order is important as it will look top to bottom and return the first one it encounters, for example: ```php From 2f22f6ea44e75ffd0c0b728c4201a0d90f3eb898 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Tue, 25 Sep 2018 09:07:54 +0100 Subject: [PATCH 8/9] Move namespace documentation heading --- docs/guides/template-locations.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/guides/template-locations.md b/docs/guides/template-locations.md index 6e6ac7569..50b748ee8 100644 --- a/docs/guides/template-locations.md +++ b/docs/guides/template-locations.md @@ -23,6 +23,8 @@ Timber::$locations = array( ); ``` +## Register your own namespaces + You can also use namespaces in your locations too, just define it as the value next to a path, for example: ```php @@ -42,8 +44,6 @@ Assuming you have a template called `menu.twig` within that namespace, you would {% include '@styleguide/menu.twig' %} ``` -## Register your own namespaces - You can also register multiple paths for the same namespace. Order is important as it will look top to bottom and return the first one it encounters, for example: ```php From 594f77c9c9eae77f62c2dbeb34d22aca939e916d Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Sat, 29 Sep 2018 21:49:43 +0100 Subject: [PATCH 9/9] Remove commented out code. array_diff() not needed as is handled by a combo of the array merge and array unique. --- lib/LocationManager.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/LocationManager.php b/lib/LocationManager.php index eaae3cc4c..091e00e07 100644 --- a/lib/LocationManager.php +++ b/lib/LocationManager.php @@ -15,7 +15,6 @@ public static function get_locations( $caller = false ) { $locs = array_merge_recursive( $locs, self::get_locations_user() ); $locs = array_merge_recursive( $locs, self::get_locations_caller( $caller ) ); //remove themes from caller - //$locs = array_diff_assoc( $locs, self::get_locations_theme() ); $locs = array_merge_recursive( $locs, self::get_locations_theme() ); $locs = array_merge_recursive( $locs, self::get_locations_caller( $caller ) ); $locs = array_map( 'array_unique', $locs ); @@ -51,7 +50,7 @@ protected static function get_locations_theme() { continue; } $theme_locs[ Loader::MAIN_NAMESPACE ][] = $root; - $root = trailingslashit( $root ); + $root = trailingslashit( $root ); foreach ( $theme_dirs as $dirname ) { $tloc = realpath( $root . $dirname ); if ( is_dir( $tloc ) ) {