Skip to content

Macro example using nested Jinja2 transforms#47

Draft
ogenstad wants to merge 1 commit intomainfrom
pog-nested-transforms-macro
Draft

Macro example using nested Jinja2 transforms#47
ogenstad wants to merge 1 commit intomainfrom
pog-nested-transforms-macro

Conversation

@ogenstad
Copy link
Contributor

@ogenstad ogenstad commented Jun 9, 2025

This PR provides an alternate approach to #46, with the difference being that we use Jinja2 macros that are imported from another file. During testing I noticed that Jinja2 tries to render the content of the template from where we import the macro when importing a macro in this way. However the context with the provided data seems to differ so I've added a workaround in the interface template where we check to see if the data variable is defined.

{%- endmacro -%}
{%- if data is defined %}
{{ render_interfaces(data.InfraInterface.edges) }}
{% endif %} No newline at end of file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this conditional if-statement all the rendering of the base template fails because data is not defined. Another approach to this would be to place all macros in a separate file. Though in that case this stand-alone interface template would only import the macro and then render the macro.

@ogenstad ogenstad marked this pull request as draft June 9, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant