Giter VIP home page Giter VIP logo

Comments (19)

NigelGreenway avatar NigelGreenway commented on May 29, 2024

@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.

 avatar commented on May 29, 2024

@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.

silentworks avatar silentworks commented on May 29, 2024

@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.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

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

from twig-view.

silentworks avatar silentworks commented on May 29, 2024

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.

 avatar commented on May 29, 2024

@silentworks, @NigelGreenway thank you very much!

from twig-view.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

@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.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

ps/ sorry for the delay...

from twig-view.

 avatar commented on May 29, 2024

@NigelGreenway, @silentworks

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

from twig-view.

silentworks avatar silentworks commented on May 29, 2024

@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.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

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

from twig-view.

 avatar commented on May 29, 2024

@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;
}

from twig-view.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

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

from twig-view.

 avatar commented on May 29, 2024

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.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

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.

 avatar commented on May 29, 2024

@NigelGreenway thank you!

from twig-view.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

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.

akrabat avatar akrabat commented on May 29, 2024

@NigelGreenway please raise the PR :)

from twig-view.

NigelGreenway avatar NigelGreenway commented on May 29, 2024

@akrabat done :)

from twig-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.