Comments (19)
@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
from twig-view.
@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):
from twig-view.
@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'
]);
from twig-view.
Apologies, I will get on to this at lunch today.
from twig-view.
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
.
from twig-view.
@silentworks, @NigelGreenway thank you very much!
from twig-view.
@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?
from twig-view.
ps/ sorry for the delay...
from twig-view.
if (is_string($namespace)) {
$loader->setPaths($path, $namespace);
} else {
$loader->addPath($path);
}
from twig-view.
@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?
from twig-view.
@ugsite: I am also confused with what your comment means?
from twig-view.
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;
}
from twig-view.
@ugsite what is the difference, so I understand it more 😊
from twig-view.
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']]);
from twig-view.
Awesome. I will do that along with the tests to reflect the code.
Cheers @ugsite
On 9 Mar 2016 12:23 pm, "Alexander K" [email protected] 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
#52 (comment).
from twig-view.
@NigelGreenway thank you!
from twig-view.
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.
from twig-view.
@NigelGreenway please raise the PR :)
from twig-view.
@akrabat done :)
from twig-view.
Related Issues (20)
- Question - Twig 3 Support HOT 10
- Slim\Views\Twig::create does not exist HOT 3
- Add support for Twig 3 in 3.x branch HOT 1
- Slim 4 Compatibility In The Pipeline? HOT 1
- Rendering no space HOT 1
- Fix incorrect declaration HOT 3
- Version on Readme HOT 1
- Twig could not be found in the server request attributes using the key "view" HOT 5
- ,
- How to get args for is_current_url()? HOT 2
- Using view outside a request/response cycle does not load the Slim/Views/TwigExtension HOT 2
- Trying to use TwigExtension template functions returns an error HOT 1
- Uncaught Twig\Error\SyntaxError: Unknown "defined" test. HOT 3
- is_current_url() doesn't take basepath into consideration HOT 20
- Support PHP 8.0
- twig->fetch() outputs to browser rather than return string HOT 8
- 3.3.0 Release date HOT 3
- `composer require slim/twig-view:^3.0` handle PHP Version Problem HOT 1
- Rendering a template from a middleware HOT 3
- Error using Twig with Slim 4 & Container HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twig-view.