Return array from a jinja macro to use in another macro

I want to combine jinja and macros so I can use a list in more than one model.

Example I have this:


{% set payment_methods = ["bank_transfer", "credit_card", "gift_card"] %}

{% for payment_method in payment_methods %}
...
{% endfor %}

But I want to place the list of payment_methods in another file so I can use the list in different models. I was trying to achieve ir this way.
Having this macro:

{% macro payment_methods_macro() %}
    select array["bank_transfer", "credit_card", "gift_card"]
{% endmacro %}

and then in my model doing:

{% set payment_methods = payment_methods_macro() %}

{% for payment_method in payment_methods %}
...
{% endfor %}

This does not work. Can anyone help me out here?

I’m not sure what’s your purpose, but for that to work you need to return a list from the payment_methods_macro, not a SELECT statement:

{% macro payment_methods_macro() %}
   {% set payment_methods = ["bank_transfer", "credit_card", "gift_card"] %}
   {{ return (payment_methods) }}
{% endmacro %}

If you want to build the list from the values on some table, that’s a bit harder, but it’s still possible using the run_query function.

Let me know if this helps

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.