Twig 用变量设置变量名 [TWIG/PHP] [英] Twig set a variable name with a variable [TWIG/PHP]

查看:33
本文介绍了Twig 用变量设置变量名 [TWIG/PHP]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Twig 中使用另一个值的值设置变量的名称?我认为它会是这样的:

How can I set a variable's name with the value of another value in Twig? I would think that It would go something like this:

{% set queCount = loop.index %}
{% for row2 in answer+queCount %}

但这行不通.还从中创建一个字符串将打破循环(因为它不搜索字符串).
我知道如何在 PHP 中执行此操作,所以为了澄清起见,这就是我想要实现的目标:

But this does not work. Also making a string from that will break the loop (because it does not search for a string).
I know how to do this in PHP so for clarification, this is what I would like to achieve:

$count = 1;
$args["answer$count"]

哪个返回

$args["answer1"]

但这一次,不是字符串而是操作性答案.

But this time, not with strings but the operant answer.

提前致谢!
Mats de Waard.

Thanks in advance!
Mats de Waard.

推荐答案

您可以使用 _context 变量

{% set queCount = loop.index %}
{% for row2 in _context['answer' ~ queCount] %}

这是关于 _context 和全局变量的文档

这篇关于Twig 用变量设置变量名 [TWIG/PHP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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