Skip to content

How to refactor Copier templates without submodules? #896

Answered by pawamoy
verdaatt asked this question in Q&A
Discussion options

You must be logged in to vote

You're looking for macros, well... Jinja has macros! https://jinja.palletsprojects.com/en/3.1.x/templates/#macros.

In a file called general in your repository root:

{% macro ecs(...) -%}
    ...
{%- endmacro %}

{% macro route53(...) -%}
    ...
{%- endmacro %}

{% macro vpc(...) -%}
    ...
{%- endmacro %}

(the dots are to be replaced with actual parameters/contents)

Then in your templated files (that you still have to duplicate everywhere, but that now require no maintenance):

{% import "general" as general %}
{{ general.ecs(...) }}
{% import "general" as general %}
{{ general.route53(...) }}
{% import "general" as general %}
{{ general.vpc(...) }}

(again, dots to be replaced with actua…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@verdaatt
Comment options

@pawamoy
Comment options

pawamoy Jan 17, 2023
Collaborator

@verdaatt
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by yajo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants