Resources »

Solutions Hub »

Automations »

Scope scripting variables

Basic variable scope with array transformations

The with command creates an isolated scope. Array operations performed inside are not visible outside.

  • 
    start:
      set:
        message@text:
          {% set numbers = range(1, 5) %}
          Outside scope numbers are: {{numbers|join(',')}}
          {% with %}
          {% set doubled = numbers|map(n => n * 2) %}
          Inside scope doubled numbers are: {{doubled|join(',')}}
          {% endwith %}
          Doubled numbers are not visible here anymore
      return:
        output@key: message
    
    
  • 
    __return:
      output: |-
        Outside scope numbers are: 1,2,3,4,5
        Inside scope doubled numbers are: 2,4,6,8,10
        Doubled numbers are not visible here anymore
    
    

Using variable mapping

Pass variables directly in the with command using a mapping.

  • 
    start:
      set:
        message@text:
          {% set numbers = range(1, 5) %}
          {% with {tripled: numbers|map(n => n * 3)} %}
          Inside mapping scope tripled numbers are: {{tripled|join(',')}}
          {% endwith %}
      return:
        output@key: message
    
    
  • 
    __return:
      output: |
        Inside mapping scope tripled numbers are: 3,6,9,12,15
    
    

Using only to restrict outer scope

The with command creates an isolated scope. Array operations performed inside are not visible outside.

  • 
    start:
      set:
        message@text:
          {% set outer = 'I am not visible' %}
          {% with {inner: 'I am visible'} only %}
          With only: {{inner}}, outer value is not accessible.
          {% endwith %}
      return:
        output@key: message
    
    
  • 
    __return:
      output: |
        With only: I am visible, outer value is not accessible.
    
    

Basic variable scope with isolation

The with command creates an isolated scope. Variables defined inside are not visible in the outer scope.

  • 
    start:
      set:
        message@text:
          {% with %}
          {% set name = 'Kina' -%}
          Hi, {{name}}!
          {% endwith %}
          {% if name is empty %}
          Where did you go?
          {% endif %}
      return:
        output@key: message
    
    
  • 
    __return:
      output: |
        Hi, Kina!
        Where did you go?