Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros should be able to call other macros #469

Closed
drewbanin opened this issue Jun 23, 2017 · 5 comments
Closed

macros should be able to call other macros #469

drewbanin opened this issue Jun 23, 2017 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@drewbanin
Copy link
Contributor

drewbanin commented Jun 23, 2017

This works if the macros are in the same file, but I think we can make this work across files by putting the underlying macro functions into the jinja context and parsing the macro sql twice. Agree @cmcarthur?

This will double down on our requirement that macros do not use this, ref, etc. I think that's ok though

@drewbanin drewbanin added the enhancement New feature or request label Jun 23, 2017
@drewbanin drewbanin self-assigned this Jun 23, 2017
@drewbanin drewbanin modified the milestone: 0.8.4 Jun 30, 2017
@cmcarthur
Copy link
Member

fixed by #466

@jranks123
Copy link

@cmcarthur @drewbanin I can't figure out from your subsequent PR how to call a macro from inside another macro when they aren't in the same file - do you have any pointers?

@drewbanin
Copy link
Contributor Author

heya @jranks123! Sure thing, this is definitely possible today. Just make sure that both of these macros live inside of files in the macros/ directory. For example:

-- macros/my_first_macro.sql

{% macro my_first_macro() -%}

  Hi {{ get_name() }}

{%- endmacro %}
-- macros/my_second_macro.sql

{% macro get_name() -%}

  Drew

{%- endmacro %}

Then, from a model:

-- models/my_model.sql

select '{{ my_first_macro()' as greeting

You should see the compiled SQL looks like:

select 'Hi Drew' as name

@darious
Copy link

darious commented Jan 28, 2021

This works great, but your final example is missing the closing }} so should be

-- models/my_model.sql

select '{{ my_first_macro() }}' as greeting

@drewbanin
Copy link
Contributor Author

ha! you are very right - sorry about that one 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants