Skip to content

Commit

Permalink
Add support for path namespaces.
Browse files Browse the repository at this point in the history
Particularly useful if using a styleguide which uses a particular
namespace.
  • Loading branch information
Ian Jenkins committed Sep 19, 2018
1 parent 8c4dac4 commit aea48b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/Loader.php
Expand Up @@ -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;
}
Expand Down
10 changes: 7 additions & 3 deletions lib/LocationManager.php
Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -142,4 +146,4 @@ protected static function get_locations_caller( $caller = false ) {
}


}
}
5 changes: 5 additions & 0 deletions tests/test-timber-loader.php
Expand Up @@ -124,5 +124,10 @@ function testTwigLoadsFromLocation(){
$this->assertEquals('<img src="" />', trim($str));
}

function testTwigLoadsFromLocationWithNamespace(){
Timber::$locations = array( 'assets' => __DIR__.'/assets' );
$str = Timber::compile('@assets/thumb-test.twig');
$this->assertEquals('<img src="" />', trim($str));
}

}

0 comments on commit aea48b9

Please sign in to comment.