Closed ghost closed 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
@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):
@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'
]);
Apologies, I will get on to this at lunch today.
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
.
@silentworks, @NigelGreenway thank you very much!
@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?
ps/ sorry for the delay...
@NigelGreenway, @silentworks
if (is_string($namespace)) {
$loader->setPaths($path, $namespace);
} else {
$loader->addPath($path);
}
@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?
@ugsite: I am also confused with what your comment means?
@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;
}
@ugsite what is the difference, so I understand it more 😊
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']]);
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.
@NigelGreenway thank you!
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.
@NigelGreenway please raise the PR :)
@akrabat done :)
How to use multiple paths per namespace?