gpt4 book ai didi

symfony - 使用变量作为 twig 中的散列键(作为 path() 或 |trans 中的参数)

转载 作者:行者123 更新时间:2023-12-03 11:44:46 24 4
gpt4 key购买 nike

是否可以动态使用 trans 过滤器或路径函数?以变量为参数?
例1:

{{ path('object_edit', { parameter_type : parameter_value }) }}

因为:我不知道“对象”是使用 id 还是 slug 进行路由

例2:
{{message|trans({ parameter_type : parameter_value }, 'TranslationDomain') }}

因为:我不知道它是 '%user%' 或 '%article%' 还是别的什么

为什么这工作正常:
{{ path('object_edit', { 'id' : parameter_value }) }}

但这不是:
{{ set parameter_type = 'id' }}
{{ path('object_edit', { parameter_type : parameter_value }) }}

最佳答案

{% set key = 'foobar' %}
{% set hash = { (key) : 'hello world' } %}
{% debug hash %}

打印:
array(1) { ["foobar"]=> string(11) "hello world" } 

用括号括起来可以解决您的问题。

关于symfony - 使用变量作为 twig 中的散列键(作为 path() 或 |trans 中的参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8893197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com