CMS Development

tjoyce
Expert reconnu | Partenaire solutions Elite
Expert reconnu | Partenaire solutions Elite

How to change a variable outside of a for loop? Scoping Issues

Résolue

This question is extremely common; How do I use a counter in a variable that was created outside of a for loop. @Jsum did a good job answering that question here. I just wanted to expand on it and make this post more "searchable" by users trying to accomplish the same thing. 

{# create the counter #}
{% set counter = {'value': 1} %}

{# create a macro that will change the counter, when called #} {% macro incrementDecrementCounter(valuechange) %} {% set changingCounter = counter.update({'value': counter.value + valuechange}) %} {% endmacro %}

{# the for...loop #} {% for n in range(10) %} {% if n % 3 == 0 %}

{# call the macro to update the counter #} {{incrementDecrementCounter(-1)}} {% else %}

{# call the macro to update the counter #} {{incrementDecrementCounter(1)}} {% endif %} <p>The loop index is: {{n}}, while the counter is: {{counter.value}}</p> {% endfor %}

@Anton@dennisedson@Stephanie-OG

1 Solution acceptée
jennysowyrda
Solution
Gestionnaire de communauté
Gestionnaire de communauté

How to change a variable outside of a for loop? Scoping Issues

Résolue
5 Réponses
gotohubspot
Membre

How to change a variable outside of a for loop? Scoping Issues

Résolue

If you will use an array ( SizeLimitingPyList: [] ) rather than object ( SizeLimitingPyMap: {} ) it would be accessible in for loop.

0 Votes
mIIwaukee
Contributeur | Partenaire solutions Diamond
Contributeur | Partenaire solutions Diamond

How to change a variable outside of a for loop? Scoping Issues

Résolue

Thanks everyone, here's a non-array approach.

{% set thisClient = {'name': 'none' } %}
{% for client in dynamic_page_hubdb_row.client %}
{% do thisClient.update({'name': client.name }) %}
{% endfor %}

{{thisClient.name}}

 

0 Votes
matt_scott
Contributeur de premier rang | Partenaire solutions Platinum
Contributeur de premier rang | Partenaire solutions Platinum

How to change a variable outside of a for loop? Scoping Issues

Résolue

I didn't think of using a macro, that's a nice solution!

I found a hacky way to do something similar but different
This was some code I used to build a dictionary:
The ‘.append’ function actually appends to the global dictionary ‘vars’, the variable inside the set is irrelevant but is needed for the hubl to run

{% set vars = [] %}
{% for item in group %}
    {% set thisVariableDoesntDoAnything = vars.append(item.variable) %}
{% endfor %}

 

Matthew Scott
Head of Development | Hubspot Solutions Architect

B2B marketing agency: Specialist B2B content marketing and demand generation for SaaS vendors and HubSpot Users | Deeply Digital | HubSpot Partner since 2010


01926 334003

deeplydigital.co.uk

3 Morton Street, Leamington Spa, CV32 5SY, UK
matt_scott
Contributeur de premier rang | Partenaire solutions Platinum
Contributeur de premier rang | Partenaire solutions Platinum

How to change a variable outside of a for loop? Scoping Issues

Résolue

Since writting the above I found the "do" function and hubspot have now added the "do" function to the docs so the code doesn't seem so hacky

{% set vars = [] %}
{% for item in group %}
    {% do vars.append(item.variable) %}
{% endfor %}
Matthew Scott
Head of Development | Hubspot Solutions Architect

B2B marketing agency: Specialist B2B content marketing and demand generation for SaaS vendors and HubSpot Users | Deeply Digital | HubSpot Partner since 2010


01926 334003

deeplydigital.co.uk

3 Morton Street, Leamington Spa, CV32 5SY, UK
jennysowyrda
Solution
Gestionnaire de communauté
Gestionnaire de communauté

How to change a variable outside of a for loop? Scoping Issues

Résolue

Thanks for sharing @tjoyce!