slimphp / Twig-View

Slim Framework view helper built on top of the Twig templating component
http://slimframework.com
MIT License
359 stars 87 forks source link

Multiple paths per namespace #52

Closed ghost closed 8 years ago

ghost commented 8 years ago

How to use multiple paths per namespace?

NigelGreenway commented 8 years ago

@ugsite You would need to pass the root directory of where your directories are stored.

See here for an example.

quick explanation:

If you have dir_1, dir_2 and dir_3 that are within the templates directory, you would do the following:


$templateDirectories = [
   'MyTemplates' =  'some/path/to/templates`,
];

$twig = new Slim\Views\Twig($templateDirectories);

In you render method, you would pass @MyTemplates/some/where/your.html.twig. If you had multiple template directories, you would do


$templateDirectories = [
   'MyTemplatesA' =  'some/path/to/templates',
   'MyTemplatesB' =  'another/path/to/templates',
];

$twig = new Slim\Views\Twig($templateDirectories);

You would then call the first templates with @MyTemplatesA/some/directory/file_a.html.twig and @MyTemplatesB/other/file_b.html.twig.

Hope this helps

ghost commented 8 years ago

@NigelGreenway thank you for answer.

In this example you use one path per namespace, but how to use multiple paths per namespace, path_1, path_2, path_3 in namespace_1 or in main namespace?

Before version 2.1.0: $twig = new Slim\Views\Twig(['path_1', 'path_2', 'path_3']); (for multiple paths in main namespace):

silentworks commented 8 years ago

@NigelGreenway can you change the $loader->addPaths() call to $loader->setPaths() and this would allow something like the previous setup.

@ugsite I didn't realise we just broke BC with this change, but it should be noted on line 53 that $path was only defined to take a string although an array worked. So I am not too sure if this is a BC break. If we use setPaths() the developer can do:

$templateDirectories = [
   'MyTemplatesA' =  [
       'some/path/to/templates', 
       'more/path/to/templates'
   ],
   'MyTemplatesB' =  'another/path/to/templates',
];

$twig = new Slim\Views\Twig($templateDirectories);

or to get default namespace you could probably do:

$twig = new Slim\Views\Twig([Twig_Loader_Filesystem:: MAIN_NAMESPACE => [
   'some/path/to/templates', 
   'more/path/to/templates'
]]);

or for the immediate moment, you can do the following:

$twig = new Slim\Views\Twig('some/path/to/templates'); 
$twig->getLoader()->setPaths([
   'additional/path/to/templates', 
   'more/path/to/templates'
]);
NigelGreenway commented 8 years ago

Apologies, I will get on to this at lunch today.

silentworks commented 8 years ago

No worries @NigelGreenway, I think it was just badly documented code on our path. There was no way to tell that Twig_Loader_Filesystem would take an array unless you looked at that class itself. We didn't seem to add that to the docblock for the constructor of Slim\View\Twig.

ghost commented 8 years ago

@silentworks, @NigelGreenway thank you very much!

NigelGreenway commented 8 years ago

@silentworks, @ugsite Can you confirm that you are happy with this fix before I submit a PR?

@silentworks I would like to do some cleaning on certain parts, I am right to do this in an entirely separate branch?

NigelGreenway commented 8 years ago

ps/ sorry for the delay...

ghost commented 8 years ago

@NigelGreenway, @silentworks

if (is_string($namespace)) {
    $loader->setPaths($path, $namespace);
} else {
    $loader->addPath($path);
}
silentworks commented 8 years ago

@ugsite don't understand fully what you are suggesting, are you saying we should use that code in the constructor instead or inside the addPaths method?

NigelGreenway commented 8 years ago

@ugsite: I am also confused with what your comment means?

ghost commented 8 years ago

@silentworks, @NigelGreenway

private function addPaths(array $paths)
{
    $loader = new \Twig_Loader_Filesystem();

    foreach ($paths as $namespace => $path) {
        if (is_string($namespace)) {
            $loader->setPaths($path, $namespace);
        } else {
            $loader->addPath($path);
        }
    }

    return $loader;
}
NigelGreenway commented 8 years ago

@ugsite what is the difference, so I understand it more 😊

ghost commented 8 years ago

We can use

$twig = new Slim\Views\Twig(['path_1', 'path_2', 'path_3']);

and

$twig = new Slim\Views\Twig(['namespace_1' => ['path_1', 'path_2']]);
NigelGreenway commented 8 years ago

Awesome. I will do that along with the tests to reflect the code.

Cheers @ugsite On 9 Mar 2016 12:23 pm, "Alexander K" notifications@github.com wrote:

We can use

$twig = new Slim\Views\Twig(['path_1', 'path_2', 'path_3']);

and

$twig = new Slim\Views\Twig(['namespace_1' => ['path_1', 'path_2']]);

— Reply to this email directly or view it on GitHub https://github.com/slimphp/Twig-View/issues/52#issuecomment-194272317.

ghost commented 8 years ago

@NigelGreenway thank you!

NigelGreenway commented 8 years ago

I am just about to submit a PR, but wondered if you wanted a look.

I have created a test, and believe that there are enough tests to cover the changes.

cc/ @ugsite @silentworks

Again, sorry for the BC break.

akrabat commented 8 years ago

@NigelGreenway please raise the PR :)

NigelGreenway commented 8 years ago

@akrabat done :)