# How to use an array inside a for statement

**URL:** <https://community.hubspot.com/t/how-to-use-an-array-inside-a-for-statement/6523>\
**Category:** CMS Development\
**Created:** [January 8, 2018, 7:19pm UTC](https://community.hubspot.com/t/how-to-use-an-array-inside-a-for-statement/6523 "2018-01-08T19:19:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leticiatm](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/leticiatm/32/35926_2.png) [@leticiatm](https://community.hubspot.com/u/leticiatm)\
**Post date:** [January 8, 2018, 7:19pm UTC](https://community.hubspot.com/t/how-to-use-an-array-inside-a-for-statement/6523/1 "2018-01-08T19:19:35Z")

</div>

I’m trying to use the gallery inside a custom module, so I need to get the module ID to get the content.

I searched and found this solution:

[https://community.hubspot.com/t5/Content-Design-Questions/Using-a-module-inside-a-custom-module/m-p/7399/highlight/true#M762](https://community.hubspot.com/t5/Content-Design-Questions/Using-a-module-inside-a-custom-module/m-p/7399/highlight/true#M762)

So I used it like this:

```auto
{% gallery "slider_gallery" label="Slider Gallery", export_to_template_context=True, no-wrapper=True %}
{% set gallery_caller = "{{widget_data" ~ "." ~ parent_custom_widget_name ~ "_slider_gallery" ~ ".slides}}" %}

{% for item in gallery_caller %}
<li class="side-img" {% if item.img_src %} style="background-image:url( {{ item.img_src }} )" {% endif %} ></li>
{% endfor %}

```

But I’m not able to use this string in a for statement

If I print the string, it’ll correctly print the gallery array. But if I use in a for statement, it throws me a warning:

Cannot resolve property ‘img\_src’ in ‘{{widget\_data.widget\_1515267612447\_slider\_gallery.slides}}’

---

<div class="post-metadata">

**Author:** ![ndwilliams3](https://avatars.discourse-cdn.com/v4/letter/n/c4cdca/32.png) [@ndwilliams3](https://community.hubspot.com/u/ndwilliams3)\
**Post date:** [February 14, 2017, 11:06pm UTC](https://community.hubspot.com/t/how-to-use-an-array-inside-a-for-statement/6523/2 "2017-02-14T23:06:46Z")

</div>

I have used this hack to achieve this. Use the set variable function to concatenate a string that includes the **parent\_custom\_widget\_name** variable\*\*.\*\* The **parent\_custom\_widget\_name** includes the appended number for the module instance on the page. Then use your custom variable where you want the content.

```auto
{% set string = "{{widget_data" ~ "." ~ parent_custom_widget_name ~ "_my_content" ~ ".value}}" %}

<p> {{string}} </p>

```

---

<div class="post-metadata">

**Author:** ![andreasmb](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/andreasmb/32/44643_2.png) [@andreasmb](https://community.hubspot.com/u/andreasmb)\
**Post date:** [February 15, 2017, 12:04am UTC](https://community.hubspot.com/t/how-to-use-an-array-inside-a-for-statement/6523/3 "2017-02-15T00:04:28Z")

</div>

Wow, that’s clever. And it works!

---

<div class="post-metadata">

**Author:** ![leticiatm](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/leticiatm/32/35926_2.png) [@leticiatm](https://community.hubspot.com/u/leticiatm)\
**Post date:** [January 8, 2018, 6:19pm UTC](https://community.hubspot.com/t/how-to-use-an-array-inside-a-for-statement/6523/4 "2018-01-08T18:19:06Z")

</div>

How can I do that in a for loop?

```auto
{% gallery "slider_gallery" label="Slider Gallery", export_to_template_context=True, no-wrapper=True %}
{% set gallery_caller = "{{widget_data" ~ "." ~ parent_custom_widget_name ~ "_slider_gallery" ~ ".slides}}" %}

{% for item in gallery_caller %}
<li class="side-img" {% if item.img_src %} style="background-image:url( {{ item.img_src }} )" {% endif %} ></li>
{% endfor %}

```

It’s getting me a warning:

Cannot resolve property ‘img\_src’ in ‘{{widget\_data.widget\_1515267612447\_slider\_gallery.slides}}’
