What config when controller is other folder and template path is inadequate - symfony

I work with Symfony 4.2
I divided my dir structure, and every part of app is in other folder.
Ex. admin part code is in src/Admin/...
- src/Admin/Controller
etc.
When I set #Template in controller method symfony tell me to situate templates in templates/
But I want to it placed in teplates/Admin/
What I should change in config.
If is other solution than set every action path in #Template(...) ?

Taken from the docs:
# config/packages/twig.yaml
twig:
# ...
paths: ["%kernel.project_dir%/resources/views"]
https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-templates-directory
This might also be interesting for you:
https://symfony.com/doc/current/templating/namespaced_paths.html#multiple-paths-per-namespace

Related

Symfony using multiple translation files for single language

I was wondering if it is possible with Symfony 3.5 to use multiple translation files for a single language when using yml files.
Currently I have something like this:
AppBundle/Resources/translations/messages.en.yml
AppBundle/Resources/translations/messages.de.yml
which contains all my translations in either language. However I was wondering if it was possible to change this to the following structure:
AppBundle/Resources/translations/en/products.yml
AppBundle/Resources/translations/en/invoices.yml
AppBundle/Resources/translations/de/products.yml
AppBundle/Resources/translations/de/invoices.yml
I have been looking but I have been unable to find some kind of solution for this. I got it working for splitting up my routes.
AppBundle/Resources/config/routing.yml
appbundle_routes:
resource: '#AppBundle/Resources/config/routing'
type: directory
Inside that folder I got all my routes split like:
AppBundle/Resources/config/routing/products.yml
AppBundle/Resources/config/routing/users.yml
AppBundle/Resources/config/routing/invoices.yml
I was wondering if it was possible to achieve the same thing with translations?
Symfony's Translator requires files to by named in format domain.locale.loader. In case you have messages.en.yml:
messages is the default name of domain, you can also specify eg. invoices
en is the locale
yml is specifying YAML loader will be used
So your proposed use is not possible to achieve with standard set of configs and functionality. However, you can split your translations to different domain files. So paths would be:
AppBundle/Resources/translations/products.en.yml
AppBundle/Resources/translations/invoices.en.yml
And when you are using translator you specify the domain in which the translation should be looked for:
$translator->trans('translated.key', [], 'invoices');
Or in Twig:
{{ 'translated.key'|trans({},'invoices') }}

Unable to find template Bundle:Default:index.html.twig

I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");

reading yaml from twig

Preface: in ez4 i remember there was a tpl function to read ini settings, we used to use this to pass specific locations or id's with which we could then render certain content.
In ezplatform I am now doing the same thing but by using the PreContentViewListener (in the PreContentViewListener read a yml file and pass into the view as params), but this doesn't feel like the correct way as the PreContentViewListener doesn't always get triggered, in custom controllers for example.
Question
Is there a native way to read yaml files from within twig templates? After searching the docs and available packagists i cannot find anything :/
If your needs are simple (i.e. reading container parameters), you can also use eZ Publish config resolver component which is available in any Twig template with ezpublish.configResolver.
You can specify a siteaccess aware parameter in format <namespace>.<scope>.<param_name>, like this:
parameters:
app.default.param.name: 'Default param value'
app.eng.param.name: 'English param value'
app.cro.param.name: 'Croatian param value'
where default, eng and cro are different eZ Publish scopes.
You can then use the config resolver to fetch the parameter in current scope with:
{{ ezpublish.configResolver.parameter('param.name', 'app') }}
If you have Legacy Bridge installed, this even falls back to legacy INI settings if no Symfony container parameter exists:
{{ ezpublish.configResolver.parameter('SiteSettings.SiteName', 'site') }}
Disclaimer: Some say that using config resolver is bad practice, but for simpler usecases it is okay, IMO.
Have a look to our CjwPublishToolsBundle.
https://github.com/cjw-network/CjwPublishToolsBundle
https://github.com/cjw-network/CjwPublishToolsBundle/blob/master/Services/TwigConfigFunctionsService.php
Here we have 2 wrapper twig functions
{{cjw_config_resolver_get_parameter ( 'yamlvariablename', 'namespace default ezsettings') }}
=> ezpublish siteaccessmatching
{{cjw_config_get_parameter( 'mailer_transport' )}}
=> core symfony yaml reader without siteaccess
You could do a lot of things in eZ 4 and not always really good for your application design. ezini was able to read the configuration from the template but now in eZ Platform and by extension Symfony you need to respect more common patterns. IMO the view should not be that smart.
Then injecting variables to the view from a listener (PreContentViewListener or your own) is not a bad idea.
You can also use the Twig Globals that could allow you to do 2 global things:
inject variables (1)
inject a service (2)
Look here: https://symfony.com/doc/current/templating/global_variables.html
(2): please don't inject the service container globally it is bad
(1): I don't remember if the Twig Globals are Site Access aware, if not injecting your own service (2) to manage access to the config might be better.
And finally, I think that the use case is not a common one:
we used to use this to pass specific locations or id's with which we could then render certain content.
Most of the time it is a bad idea to pass ids coming from the configuration to render something, it is much better to organize the content structure to let you pull the location you want using the PHP API. (no id in configuration no hassle with dev, stage, preprod and prod architecture)

Where is #Twig defined in symfony

I am new to symfony.While going through an error template I came across value a value like
#Twig/Exception/traces_text.html.twig
Just wanted to know where is #Twig defined in symfony framework
Just google twig traces_text.html.twig and you'll see the file here: https://github.com/symfony/twig-bundle/blob/master/Resources/views/Exception/traces_text.html.twig
symfony/twig-bundle/Resources/views/Exception/traces_text.html.twig
Or use your IDE and search the vendor/ folder for the traces_text.html.twig file until you find it.

Symfony How to doing translate jordillonch / CrudGeneratorBundle

I have in my template something like that
<h1>Workers {{ 'views.index.list'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</h1>
And I want to translate this but I don't know how. I guess have to create a file with translations but where save it ,how name a file and what write inside?
In app/config/config.yml I have 'translator: { fallback: "%locale%" }'
In 'symfony/app/config/parameters.yml' defined locale parameter "locale:pl"
First look at the bundle's Resources / translations folder. It looks loke this:
JordiLlonchCrudGeneratorBundle.ca.yml
JordiLlonchCrudGeneratorBundle.en.yml
JordiLlonchCrudGeneratorBundle.es.yml
Then create a file JordiLlonchCrudGeneratorBundle.pl.yml (if you want to translate to the pl local) and put it to the app/Resources/JordiLlonchCrudGeneratorBundle/translations directory.
Use one of the default translation files as a pattern for your translation, translate all text you need and save it to the pl translation file you've just created.
See also: http://symfony.com/doc/2.1/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-validation-etc

Resources