我想在 TWIG 中生成动态变量并获取值
I want to genrate dynamic variable in TWIG and fetch value
我想设置动态变量并获取该变量的值。我已将 "orderDetails" 传递给我的树枝模板。
$customerProfile = array(
'ns1' => $customerProfile->getNs1(),
'ns2' => $customerProfile->getNs2(),
'custId' => $customerProfile->getCustId()
)
return $this->render('PortalBundle::popup.html.twig', array(
'orderDetails' => $customerProfile
));
像这样获取 {{orderDetails.ns1}}、{{orderDetails.ns2}} 这样的变量,但我有 15 到 16 个像这样的变量,我想在循环中获取这个变量。
我有这样的代码
{% for i in 1..13 %}
{% set nsOrd = 'orderDetails.ns'~i %}
{% if nsOrd %}
{{nsOrd}}
{% endif %}
{% endfor %}
我想获取变量 {{orderDetails.ns1}} 和其他变量。给我你的建议。
谢谢 Yoshi,我已经找到数组中的变量了。感谢您的支持,我已经找到并使用它成功地获得了输出。
{% for i in 1..13 %}
{% if orderDetails['ns' ~ i] is defined %}
{{orderDetails['ns' ~ i]}}
{% endif %}
{% endfor %}
我想设置动态变量并获取该变量的值。我已将 "orderDetails" 传递给我的树枝模板。
$customerProfile = array(
'ns1' => $customerProfile->getNs1(),
'ns2' => $customerProfile->getNs2(),
'custId' => $customerProfile->getCustId()
)
return $this->render('PortalBundle::popup.html.twig', array(
'orderDetails' => $customerProfile
));
像这样获取 {{orderDetails.ns1}}、{{orderDetails.ns2}} 这样的变量,但我有 15 到 16 个像这样的变量,我想在循环中获取这个变量。
我有这样的代码
{% for i in 1..13 %}
{% set nsOrd = 'orderDetails.ns'~i %}
{% if nsOrd %}
{{nsOrd}}
{% endif %}
{% endfor %}
我想获取变量 {{orderDetails.ns1}} 和其他变量。给我你的建议。
谢谢 Yoshi,我已经找到数组中的变量了。感谢您的支持,我已经找到并使用它成功地获得了输出。
{% for i in 1..13 %}
{% if orderDetails['ns' ~ i] is defined %}
{{orderDetails['ns' ~ i]}}
{% endif %}
{% endfor %}