简体   繁体   中英

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 :/

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)

If your needs are simple (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM