gpt4 book ai didi

php - 选择元素到 foreach Twig 模板中

转载 作者:行者123 更新时间:2023-12-02 22:00:25 26 4
gpt4 key购买 nike

我在 Symfony2 上使用 Twig,我在参数中有 2 个数组:

我的 Controller :

return $this->render('MyBundle:Default:index.html.twig',
array('checked' => $boxChecked,
'choices' => $choices));

Vars 'checked' 和 'choices' 是两个数组,我想显示 $checked[$choices[$i]] 的值与 true 或 false 进行比较,以将 checked 或 not 应用到 twig tpl 的输入中。

这是我的代码,但不起作用:

{% for choice in choices %}

{% if checked.{{choice}} == true %}

<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" /></div>

{% else %}

<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" checked="checked" /> </div>

{% endif %}

{% endfor %}

错误是:“MyBundle:Default:index.html.twig”中的预期名称或编号在第 22 行(500 内部服务器错误)

第 22 行是:{% if checked.{{choice}} == true %}

我不知道我是如何检查的。(我的 VAR CHOICE 到我的 foreach CHOICES 中)到 twig tpl 中?

最佳答案

您必须改用括号语法:

    {% for choice in choices %}

{% if checked[choice] == true %}

<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" /></div>

{% else %}

<div class="choice">{{ choice|capitalize }} <input type="checkbox" id="{{ choice }}" checked="checked" /> </div>

{% endif %}

{% endfor %}

关于php - 选择元素到 foreach Twig 模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17041208/

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