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

how to 'wrap' a macro #1464

Open
joelanman opened this issue Apr 12, 2024 · 0 comments
Open

how to 'wrap' a macro #1464

joelanman opened this issue Apr 12, 2024 · 0 comments

Comments

@joelanman
Copy link

joelanman commented Apr 12, 2024

Thanks for Nunjucks! On the GOV.UK Prototype Kit we use it extensively, and one thing we do is load in macros through a custom plugin system.

We do that in a base layout file (lets say that is called base.njk).

I'm trying to wrap some macros from one plugin to add functionality to them (set some default values). So I want to do something like this (slightly simplified):

{% set _myComponent = myComponent %}

{% macro myComponent(option1, option2) %}
  {% set option2 = option2 or "value2" %}
  {{ _myComponent(option1, option2) }}
{% endmacro %}

If I do this in the same file that myComponent is imported in, this works. However due to constraints in the plugin system I can't do it there, so I'm trying to do it in a child layout that extends base.njk. If I do that it stops working, it seems myComponent is available as a macro to run, but can't be reassigned (it says myComponent is undefined). So doing it with a new name works but isn't what I need:

{% macro myNewComponent(option1, option2) %}
  {% set option2 = option2 or "value2" %}
  {{ myComponent(option1, option2) }}
{% endmacro %}

Is there a way to achieve 'wrapping' like this in a child layout, or method other than reassigning it in the same layout?

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

No branches or pull requests

1 participant