Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays {{TWIG}}定义动态数组名_Arrays_Twig - Fatal编程技术网

Arrays {{TWIG}}定义动态数组名

Arrays {{TWIG}}定义动态数组名,arrays,twig,Arrays,Twig,我正在Shopwired平台上使用{{TWIG}}。硬编码到其中一个模板中的是图库 {# Render carousel #} {% set gallery_name = 'global.theme.settings.gallery_location %} 这当然是工作和伟大的,但我想在此基础上扩展,使一些动态画廊的网站我正在建设 我已经想出了如何附加/前置页面标题来构建它 {# Render carousel #} {% set gallery_name = 'global.theme.se

我正在Shopwired平台上使用{{TWIG}}。硬编码到其中一个模板中的是图库

{# Render carousel #}
{% set gallery_name = 'global.theme.settings.gallery_location %}
这当然是工作和伟大的,但我想在此基础上扩展,使一些动态画廊的网站我正在建设

我已经想出了如何附加/前置页面标题来构建它

{# Render carousel #}
{% set gallery_name = 'global.theme.settings.gallery_' ~ title|lower %}
{% set images = gallery_name %}
当我使用{images}回显结果时,它会输出字符串名“global.theme.settings.gallery_location”(这对于我所在的页面来说是正确的),而不是预期和期望的“Array”

以下内容必须几乎正确,但它仅从数组中输出一项

{# Render carousel #}
{% set gallery_name = 'global.theme.settings.gallery_' ~ title|lower %}
{% set images = [gallery_name] %}

我快到了。。我做错了什么?

要使用动态键,你不能使用
符号来访问属性,因为编译器无法处理。使用
属性
数组
符号访问数据

{% set arr = {
     foo : {
         bar : {
             foobar : {
                 dynamic_index : 42,
             }
         }
     }
} %}

{% set key = 'dynamic' %}

{{ attribute(arr['foo']['bar']['foobar'], key~'_index') }} {# 42 #}

{{ arr['foo']['bar']['foobar'][key~'_index'] }} {# 42 #}

使用来完成此任务有没有机会扩展此任务?我对TWIG还不熟悉,实际上我尝试了一些属性,但没有完成任何事情。