来自Twig中_multiple_个变量的变量名 [英] Variable name from _multiple_ variables in Twig

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

问题描述

我正在使用Opencart,我有一个来自php较旧版本的非常有用的插件.我注意到树枝并不难,我试图将php代码转换为树枝.简单的ifs/for循环/echos很简单,但是原始作者使用动态创建的变量名.

I'm using Opencart and I have a really useful plugin from an older version in php. I noticed that twig isn't that hard and I tried to convert the php code into twig. Simple ifs/for loops/echos were easy enough, but the original author uses dynamically created variable names.

当动态部分仅来自一个变量时,我已经看到了使用属性和_context的示例.但是,当变量名称包含2个,3个或更多的可变部分时,会发生什么?

I have seen examples using attribute and _context when the dynamic part comes from just one variable. But what happens when the variable name consists of 2, 3, or more variable parts?

这是一个实际的例子(还有更多类似的例子)

This is an actual example (and there are many more like this)

${'var_' . $extension['name'] . '_' . $geo_zone['geo_zone_id'] . '_' . $customer_group['customer_group_id'] . '_order_total_sort_order'};

这是可以实现的,还是一种与树枝完全不兼容的思维方式?

Is this something that can be achieved or is it a way of thinking totally incompatible with twig?

我了解我们如何使用动态创建的变量的值.但是是否有可能在不首先了解各种组件的情况下对其进行初始化?还是检查是否存在这样的变量?在我的特殊情况下,变量是由用户定义的字段创建的,然后可以自由使用,也要通过if检查.

I understand how we use the value of a dynamically created variable. But is it possible to initialize it without knowing the various components in the first place? Or check if such a variable even exists? In my particular case the variables are created by user defined fields and then used freely, also passing through an if check.

<select name="var_<?= $geo_zone['geo_zone_id']; ?>_<?= $group['group_id']; ?>_tax_class_id"> <option value="0" <?php if(${'var_' . $geo_zone['geo_zone_id'] . '_' . $group['group_id'] . '_tax_class_id'} == 0){ echo 'selected'; } ?>><?= $text_none; ?></option> </select>

<select name="var_<?= $geo_zone['geo_zone_id']; ?>_<?= $group['group_id']; ?>_tax_class_id"> <option value="0" <?php if(${'var_' . $geo_zone['geo_zone_id'] . '_' . $group['group_id'] . '_tax_class_id'} == 0){ echo 'selected'; } ?>><?= $text_none; ?></option> </select>

老实说,我不知道如何将其变成树枝.

I honestly have no idea how this can be turned into twig.

推荐答案

您只是将它们连在一起了?

You just concat them all?

{% set long_variable_name_here = 'foo' %}


{% set long = 'long' %}
{% set variable = 'variable' %}
{% set name = 'name' %}
{% set here = 'here' %}


{{ attribute(_context, long~'_'~variable~'_'~name~'_'~here) }}

演示

{% set var_1_1_tax_class_id = 0 %}

<select name="var_{{ geo_zone['geo_zone_id'] | default(1) }}_{{ group['group_id']|default(1) }}_tax_class_id">
    <option value="0"{{ attribute(_context, 'var_'~geo_zone['geo_zone_id']|default(1)~'_'~group['group_id']|default(1)~'_tax_class_id') | default(-1) == 0 ? ' checked' }}>{{ text_none | default('text') }}</option>
</select>

这篇关于来自Twig中_multiple_个变量的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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