cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
192
Views
1
Helpful
2
Replies

Reuse CLI Template Variables outside for loop

Tobias Heisele
Level 1
Level 1

Hi Community,

is it possible to reuse a Variable that has been set inside a for loop after the loop ended?

{% for interface in __interfaces %}
  {% if something matches %}
    {% set MyVariable = "This is a Test" %}
  {% endif %}
{% endfor %}

{{ MyVariable }}

1 Accepted Solution

Accepted Solutions

Tobias Heisele
Level 1
Level 1

Got it (thx to stackoverflow) using a global dictionary

{% set global = {} %}
{% for interface in __interfaces %}
  {% if something matches %}
    {% set _ = global.update({MyVariable:"This is a Test"}) %}
  {% endif %}
{% endfor %}

{% set MyVariable = global["MyVariable"] %}
{{ MyVariable }}

View solution in original post

2 Replies 2

Tobias Heisele
Level 1
Level 1

Got it (thx to stackoverflow) using a global dictionary

{% set global = {} %}
{% for interface in __interfaces %}
  {% if something matches %}
    {% set _ = global.update({MyVariable:"This is a Test"}) %}
  {% endif %}
{% endfor %}

{% set MyVariable = global["MyVariable"] %}
{{ MyVariable }}

DJW487
Level 1
Level 1

Thanks. I always like seeing little things like this.