Alternative branch path for Silex controller provider

I am creating a new controller provider, which I will use in several projects, and I need to have a couple of twig patterns inside the directory of this controller provider for use on some routes of this provider. I want not to copy all the template files to the project directory for each project that this controller provider will use (to have it in the twig.path directory), so my question is this:

How to render a template that is not in the twig.path directory? Can I tell the twin service provider to display a specific path, for example __DIR__.'/views/some.template.twig' in the controller provider file?

+4
source share
2 answers

You can access the twig loader using $ app ['twig.loader.filesystem'].

For example, put this in your controller class in the connection method

 $app['twig.loader.filesystem']->addPath( $pathToTemplates ); 

There is also Twig_Loader_Filesystem :: prependPath ()

You can also specify a namespace for paths. To do this, simply add the namespace name as the second parameter, for example, "NAMESPACE". In this case, you can access your templates $app['twig']->render('@NAMESPACE/pathToATemplate'); Note @ is a char in front of the namespace when it is rendered.

+4
source

Something like this should work

 $app['twig'] = $app->share($app->extend('twig', function($twig) use ($yourNewPath) { $twig->addLoader(new \Twig_Loader_Filesystem($yourNewPath)); return $twig; })); 
+1
source

Source: https://habr.com/ru/post/1446889/


All Articles