How to declare variable in macros

I tried below to initiate variable in macros but it not working as expected

{% macros variable_pass(var1,var2,var3) %}
var4=var1.replace(‘dev’,‘prd’)

{% set script %}
        select var4
    {% endset %}

{% do run_query(script) %}

{% endmacros %}

its failing not sure what to do…

You have to wrap it in curly brackets . Control structures and initialization should be wrapped up in {% %}. You can evaluate an expression or variable with {{ }}.

{% macro simple_replace(foo) %}
{% set bar = foo | replace(‘prod’,‘dev’) %}
{% set query %}
Select {{ bar }}
{% endset %}
{% do run_query(query) %}
{% endmacro %}

I haven’t tasted this as I am on my phone.
I suggested to go thourgh basics of jinja

Good luck!

1 Like

How to declare multiple variables in Jinja without writing multiple set unset code

Try to use commas to separate the variables and values. Think that should work. Cant test it now.

{% set foo, bar = ‘foo’, ‘bar’ %}

1 Like