Specify alternative backup paths for Symfony to find package branch patterns

Main question

How can you make symfony in non-standard directories to find the β€œbest” (customizable) Twig template to load for package presentation?

Symfony docs says that by default it looks in two places: overrides the Twig template

When AcmeBlogBundle: Blog: index.html.twig is called, Symfony actually looks in two different places for the template:

Application / Resources / AcmeBlogBundle / views / Blog / index.html.twig SRC / Acme / BlogBundle / Resources / views / Blog / index.html.twig

But this discusses how you override the provider package. In my case, I have my own packages in my / src /, which I want to rewrite to each design template or to a specific client. It should look:

Client: /var/www/vhosts/{ID}/src Template: /var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/src 

Twig Loader has a convenient way to add paths:

 $templatePath = '/var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/Resources/views'; $this->container->get('twig.loader')->prependPath($templatePath, 'template'); 

This allows me to register an alternative path to the template resources so that I can visualize the template shell as follows:

{% extends '@ template / shell / shell.html.twig'%}

But what about when I want to rewrite a package template like

 Original: /var/www/core/cms/src/Gutensite/MenuBundle/Resources/views/Menu.html.twig Custom: /var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/src/Gutensite/MenuBundle/Resources/views/Menu.html.twig 

How to register a generic / src / file so that Symfony looks there for all links to the vendor's bundle path, for example. when trying to render @GutensiteMenu/Menu.html.twig will first look in the user directory 1) Client , 2) Template , 3) by default with this name.

Required assets

Since my TemplateBundle / Templates / Admin / Resources / is in a non-standard location, assetic will not upload them to a shared directory (or create symbolic links) ... so I'm not sure how to make assetic dynamically find these files.

I am also not sure how to load assets that are in these other places, for example. this does not work:

 {% stylesheets '@GutensiteTemplateBundle/Templates/Admin/Resources/public/css/site.css' %} <link rel="stylesheet" href="{{ asset_url }}"> {% endstylesheets %} 

Presumably because it is not reset.

Why do I need it?

I am creating a hosted CMS, which is the main provider that contains various packages with controllers and templates, for example. /Gutensite/CmsBundle , Gutensite/ArticleBundle .

Based on the selected "Design Template" for the site, the design template is referenced in the TemplateBundle, for example. /TemplateBundle/Templates/Admin (a template called "Admin"). TemplateBundle should be able to redefine the main controllers or views.

I registered the Gutensite / TemplateBundle / Templates / Admin / src / folder as an alternative namespace for the Composer / autoload.php application, so that the controllers can be overwritten if they have the same Gutensite namespace (and this works fine):

 // in my primary controller: $loader = $GLOBALS['loader']; $loader->add('Gutensite', '/var/www/core/cms/src/Gutensite/TemplateBundle/Templates/Admin/src', true); 

NOTE. I could register each individual template as a package, and this would theoretically allow me to redefine the controllers and packages using the symfony built-in methods. But this will add a lot of "bundles" that are not really traditional ties. I also need the above solution to point to alternative template paths, because I also need the ability to point to client custom files located outside the normal symfony root directory (which I successfully do with the namespace autoloader paths for controllers).

+6
source share
1 answer

You can use the path option in the branch configuration, with the same namespace for 2 paths.

 twig: # ... paths: "%kernel.root_dir%/../src/Gutensite/MenuBundle/Resources/views": templates "%kernel.root_dir%/../src/Gutensite/TemplateBundle/Templates/Admin/src/Gutensite/MenuBundle/Resources/views": templates 

Then, using @templates/Menu.html.twig , first find Menu.html.twig in MenuBundle, then in TemplateBundle.

Thus, TwigExtension logs the paths, so the loader searches in app/Resources/views , and then in the package views.

+1
source

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


All Articles