CMS Development

wnell
Participant

Setting a variable in HubL

SOLVE

I am not sure if this is by design or a bug in your implementation but it certainly does not seem to be consistent. I have narrowed it down. The below snippet works as expected:

{% set queryparam2 = "initial" %} 
{% if 1 == 1 %}
{% set queryparam2 = queryparam2 ~ "&type=111" %}
{% endif %}
{{queryparam2}}

 Above prints "initial&type=111" as expected, meaning one can change a variable's value inside an if block. However, introducing a for loop:

{% set queryparam2 = "initial" %} 
{% if 1 == 1 %} 
  {% set queryparam2 = queryparam2 ~ "&type=111" %} 
  {% for part in [1] %}
     {% set queryparam2 = queryparam2 ~ "&type=222" %} 
  {% endfor %} 
{% endif %} 

{{queryparam2}}

 Above prints "initial&type=111" which is NOT as expected, it should be "initial&type=111&type=222", meaning one can NOT change a variable's value inside a for block and access it outside that block, even with the variable defined outside the scope of the for block. Comments?

0 Upvotes
1 Accepted solution
ndwilliams3
Solution
Key Advisor

Setting a variable in HubL

SOLVE

It's a known limitation. Variables within a for loop are only accessible within the for loop.

View solution in original post

0 Upvotes
3 Replies 3
ndwilliams3
Solution
Key Advisor

Setting a variable in HubL

SOLVE

It's a known limitation. Variables within a for loop are only accessible within the for loop.

0 Upvotes
ndwilliams3
Key Advisor

Setting a variable in HubL

SOLVE

Can you explain what you are trying to do? Maybe there is a workaround.

0 Upvotes
wnell
Participant

Setting a variable in HubL

SOLVE

Thanks for the info.  That is what I thought.

 

I did implement a workaround but it does make life a bit harder.

0 Upvotes