symfonytwigyamlezpublishezplatform

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


Solution

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