Freddy
Freddy

Reputation: 857

HuBL: Using for loop variable outside for loop

I have created a template partial file where I'm defining all variables in that file. I then want to reference a variable in custom modules. However, the for loop loop closes in the partial file, so when I reference it in the HTML of a custom module, it'll be out of scope.

Is there any way to use for loop variables outside the loop?

Demo:

Here is my template partial file:

<!--
  templateType: "global_partial"
  isAvailableForNewContent: false
-->

{% set table = hubdb_table_rows(table_id_here) %}

{% for row in table %}
  {% set firstname  = row.first_name %}
{% endfor %}

And here is how I'm calling the variable in my markup:

<div class="hero">
    {% include "/template_partial_file.html" %}
    <h1>{{ firstname }}</h1>
</div>

Upvotes: 0

Views: 2203

Answers (1)

PiersG
PiersG

Reputation: 31

You can't access a variable set inside a for loop outside of said loop.

You can set up an array outside of a loop and push information from within the loop to it, e.g.

{% set names = [] %}

{% for row in table %}
  {% do names.append(row.first_name) %}
{% endfor %}

Upvotes: 1

Related Questions