简体   繁体   English

从树枝上读yaml

[英]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. 前言:在ez4中,我记得有一个tpl函数可以读取ini设置,我们曾经使用它来传递特定位置或id,然后可以使用它们渲染某些内容。

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. 在ezplatform中,我现在正在执行相同的操作,但是通过使用PreContentViewListener(在PreContentViewListener中读取yml文件并将其作为参数传递到视图中),但这并不像是正确的方式,因为PreContentViewListener并不总是被触发,例如在自定义控制器中。

Question Is there a native way to read yaml files from within twig templates? 问题是否存在从树枝模板内读取yaml文件的本地方法? After searching the docs and available packagists i cannot find anything :/ 在搜索了文档和可用的包装师之后,我什么都找不到:/

Have a look to our CjwPublishToolsBundle. 看看我们的CjwPublishToolsBundle。 https://github.com/cjw-network/CjwPublishToolsBundle https://github.com/cjw-network/CjwPublishToolsBundle/blob/master/Services/TwigConfigFunctionsService.php https://github.com/cjw-network/CjwPublishToolsBundle https://github.com/cjw-network/CjwPublishToolsBundle/blob/master/Services/TwigConfigFunctionsService.php

Here we have 2 wrapper twig functions 这里我们有2个包装树枝功能

{{cjw_config_resolver_get_parameter ( 'yamlvariablename', 'namespace default ezsettings') }}

=> ezpublish siteaccessmatching => ezpublish siteaccessmatching

{{cjw_config_get_parameter( 'mailer_transport' )}}

=> core symfony yaml reader without siteaccess =>没有站点访问权限的核心Symfony Yaml阅读器

You could do a lot of things in eZ 4 and not always really good for your application design. 您可以在eZ 4中做很多事情,但并不总是非常适合您的应用程序设计。 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. ezini能够从模板读取配置,但是现在在eZ Platform中,并且通过扩展Symfony,您需要尊重更常见的模式。 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. 然后从侦听器(PreContentViewListener或您自己的侦听器)向视图中注入变量并不是一个坏主意。

You can also use the Twig Globals that could allow you to do 2 global things: 您还可以使用Twig Globals,它可以让您执行2种全局操作:

  • inject variables (1) 注入变量(1)
  • inject a service (2) 注入服务(2)

Look here: https://symfony.com/doc/current/templating/global_variables.html 在这里查看: https//symfony.com/doc/current/templating/global_variables.html

(2): please don't inject the service container globally it is bad (2):请不要在全局范围内注入服务容器,这是不好的

(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. (1):我不记得Twig Globals是否了解站点访问,如果不注入您自己的服务(2)来管理对配置的访问可能会更好。

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. 我们曾经使用它来传递特定的位置或ID,然后可以使用它们呈现某些内容。

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. 在大多数情况下,传递来自配置的ID来呈现某些东西是个坏主意,最好组织内容结构,让您使用PHP API提取所需的位置。 (no id in configuration no hassle with dev, stage, preprod and prod architecture) (无需配置ID,无需担心开发,阶段,预生产和生产体系结构)

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 . 如果您的需求很简单(即读取容器参数),则还可以使用ezpublish.configResolver在任何Twig模板中提供的eZ Publish配置解析器组件。

You can specify a siteaccess aware parameter in format <namespace>.<scope>.<param_name> , like this: 您可以使用格式<namespace>.<scope>.<param_name>来指定可访问站点的参数,如下所示:

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. 其中defaultengcro是不同的eZ Publish范围。

You can then use the config resolver to fetch the parameter in current scope with: 然后,您可以使用config解析器通过以下方式获取当前作用域中的参数:

{{ 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: 如果您安装了Legacy Bridge,则在不存在Symfony容器参数的情况下,它甚至可以退回到旧版INI设置:

{{ ezpublish.configResolver.parameter('SiteSettings.SiteName', 'site') }}

Disclaimer: Some say that using config resolver is bad practice, but for simpler usecases it is okay, IMO. 免责声明:有人说使用config resolver是不好的做法,但是对于更简单的用例,IMO是可以的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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