Twig 中的动态变量,例如? [英] Dynamic variable in Twig, example?

查看:30
本文介绍了Twig 中的动态变量,例如?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白 Twig 中的属性函数是如何工作的.有人可以帮我举个例子吗?

I don't quite understand how the attribute function in Twig works. Can somebody help me with an example?

我在 SQL 中有一个名为动态的字段.我可以是例如field27",但我不知道号码,号码保存在 radio.id 中.我想做这样的事情:

I have a field in a SQL that is named dynamic. I could be eg "field27", but I don't know the number, the number is saved in radio.id. I would like to do someting like this:

{% for radio in gruppeType.radios %}
<td><!-- value of "field" + radio.id--></td>
{% endfor %}

如何使用 field + radio.id 作为树枝变量的名称?

How can I use field + radio.id as the name of the twig-variable?

推荐答案

您可以使用变量构建字段名称,然后在 属性函数 访问对象/数组中的数据.例如:

You can build the field name with a variable, then use it in the attribute function to access the data within the object/array. As example:

{% set fieldName = "field" ~ radio.id %}

{{ attribute(gruppeType, fieldName) }}

可以在 this twigfiddle

希望这会有所帮助.

这篇关于Twig 中的动态变量,例如?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆