<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to change a variable outside of a for loop? Scoping Issues in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/286490#M13286</link>
    <description>&lt;P&gt;I didn't think of using a macro, that's a nice solution!&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I found a hacky way to do something similar but different&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This was some code I used to build a dictionary:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;{% set vars = [] %}
{% for item in group %}
    {% set thisVariableDoesntDoAnything = vars.append(item.variable) %}
{% endfor %}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Aug 2019 15:35:23 GMT</pubDate>
    <dc:creator>matt_scott</dc:creator>
    <dc:date>2019-08-16T15:35:23Z</dc:date>
    <item>
      <title>How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/211117#M8805</link>
      <description>&lt;P&gt;This question is&amp;nbsp;extremely common; How do I use a counter in a variable that was created outside of a for loop.&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/3688"&gt;@Jsum&lt;/a&gt;&amp;nbsp;did a good job answering that &lt;A href="https://community.hubspot.com/t5/Content-Design-Questions/How-to-assign-value-to-an-array-in-a-for-loop/m-p/32441#M3133" target="_self"&gt;question here&lt;/A&gt;. I just wanted to expand on it and make this post more "searchable" by users trying to accomplish the same thing.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{# create the counter #}&lt;BR /&gt;{% set counter = {'value': 1} %}&lt;BR /&gt;&lt;BR /&gt;{# create a macro that will change the counter, when called #}
{% macro incrementDecrementCounter(valuechange) %}
  {% set changingCounter = counter.update({'value': counter.value + valuechange}) %}
{% endmacro %}&lt;BR /&gt;&lt;BR /&gt;{# the for...loop #}
{% for n in range(10) %}
  {% if n % 3 == 0 %}&lt;BR /&gt;    &lt;BR /&gt;    {# call the macro to update the counter #}
    {{incrementDecrementCounter(-1)}}
  {% else %}&lt;BR /&gt;&lt;BR /&gt;    {# call the macro to update the counter #}
    {{incrementDecrementCounter(1)}}
  {% endif %}
  &amp;lt;p&amp;gt;The loop index is: {{n}}, while the counter is: {{counter.value}}&amp;lt;/p&amp;gt;
{% endfor %}&lt;/PRE&gt;&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/17186"&gt;@Anton&lt;/a&gt;,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/13982"&gt;@dennisedson&lt;/a&gt;,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/61659"&gt;@Stephanie-OG&lt;/a&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 20:23:52 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/211117#M8805</guid>
      <dc:creator>tjoyce</dc:creator>
      <dc:date>2018-10-10T20:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/211335#M8835</link>
      <description>&lt;P&gt;Thanks for sharing&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/2450"&gt;@tjoyce&lt;/a&gt;!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 21:22:57 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/211335#M8835</guid>
      <dc:creator>jennysowyrda</dc:creator>
      <dc:date>2018-10-11T21:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/286490#M13286</link>
      <description>&lt;P&gt;I didn't think of using a macro, that's a nice solution!&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I found a hacky way to do something similar but different&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This was some code I used to build a dictionary:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;{% set vars = [] %}
{% for item in group %}
    {% set thisVariableDoesntDoAnything = vars.append(item.variable) %}
{% endfor %}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 15:35:23 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/286490#M13286</guid>
      <dc:creator>matt_scott</dc:creator>
      <dc:date>2019-08-16T15:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/395069#M20171</link>
      <description>&lt;P&gt;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&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;{% set vars = [] %}
{% for item in group %}
    {% do vars.append(item.variable) %}
{% endfor %}&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 21 Dec 2020 09:32:44 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/395069#M20171</guid>
      <dc:creator>matt_scott</dc:creator>
      <dc:date>2020-12-21T09:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/505871#M25401</link>
      <description>&lt;P&gt;Thanks everyone, here's a non-array approach.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;{% set thisClient = {'name': 'none' } %}
{% for client in dynamic_page_hubdb_row.client %}
{% do thisClient.update({'name': client.name }) %}
{% endfor %}

{{thisClient.name}}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Oct 2021 18:40:08 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/505871#M25401</guid>
      <dc:creator>mIIwaukee</dc:creator>
      <dc:date>2021-10-05T18:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/1043645#M40715</link>
      <description>&lt;P&gt;If you will use an array (&amp;nbsp;&lt;SPAN&gt;SizeLimitingPyList: []&amp;nbsp;&lt;/SPAN&gt;) rather than object (&amp;nbsp;&lt;SPAN&gt;SizeLimitingPyMap: {} ) it would be accessible in for loop.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2024 08:47:37 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/1043645#M40715</guid>
      <dc:creator>gotohubspot</dc:creator>
      <dc:date>2024-09-20T08:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/1194585#M44001</link>
      <description>&lt;P&gt;I spent a whole day pulling my hair out until I came across this article.&amp;nbsp; Maybe there are good reason to not allow outer variable to be set inside the for loop with a good old set statement.&amp;nbsp; But it's super counter intuitive (based on perhaps 99% of other languages out there).&amp;nbsp; Big lame factor here imho.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Aug 2025 00:37:29 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/1194585#M44001</guid>
      <dc:creator>darveesh</dc:creator>
      <dc:date>2025-08-29T00:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to change a variable outside of a for loop? Scoping Issues</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/1194633#M44004</link>
      <description>&lt;P&gt;Hi &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/945161"&gt;@darveesh&lt;/a&gt;&lt;/SPAN&gt; and thank you for sharing your experience and feedback with the HubSpot Community!&lt;BR /&gt;&lt;BR /&gt;I completely understand how frustrating it can be to run into unexpected scoping behavior, especially when it feels counter to conventions in other languages.&lt;BR /&gt;&lt;BR /&gt;I'd recommend submitting this via the &lt;A href="https://developers.hubspot.com/feedback" target="_blank"&gt;HubSpot Developer Feedback Form&lt;/A&gt;, then the right team can have their eyes on it.&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We’re always working to improve, and your feedback about the developer experience is important.&lt;BR /&gt;&lt;BR /&gt;Thank you again for being part of the HubSpot Community!&lt;BR /&gt;&lt;BR /&gt;Have a great weekend!&lt;BR /&gt;Bérangère&lt;/P&gt;</description>
      <pubDate>Fri, 29 Aug 2025 06:46:17 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-change-a-variable-outside-of-a-for-loop-Scoping-Issues/m-p/1194633#M44004</guid>
      <dc:creator>BérangèreL</dc:creator>
      <dc:date>2025-08-29T06:46:17Z</dc:date>
    </item>
  </channel>
</rss>

