Using for loop variable (defined in template partial) outside for loop
I have created atemplate partialfile where I'm defining all variables based on a for loop. I then want to reference a variable (that is defined in that file) incustom modules.
However, thefor loop closes in the partial file, so when I reference a variable 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 (and what I want to acheive):
<div class="hero"> {% include "/template_partial_file.html" %}
<h1>{{ firstname }}</h1>
</div>
Using for loop variable (defined in template partial) outside for loop
@Amit_95 as long as you create the variable outside the loop, you can then update the variable value inside the loop. Then when you reference the variable again outside the loop it will have the updated value.
Using for loop variable (defined in template partial) outside for loop
Hi @sharonlicari (thanks for the tag!) and @Amit_95, find below my thoughts about this.
As per Hubspot's documentation, any variables defined within loops are limited to the scope of that loop and cannot be called from outside of the loop.
With that being said, I believe you should use a different approach to achieve your goal.
As @psdtohubspot said, could you please provide us with more infomation about what you want to achieve so we can help you finding alternative solutions?