-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
Fix included macros aren't working #632
Conversation
I was thinking about how this could be an issue when multiple files are included, or when a macro would then overwrite and existing property with the same name (from another template). Or you think you are in an isolated template but your local macro will replace the RootScope property with the same name. I checked Jinja (where I think I took the idea from, and why this feature is behind a flag as it's not standard). https://jinja.palletsprojects.com/en/2.10.x/templates/#import Do you think this could be a better solution? Local macros don't have to be imported so there is no breaking change. It would only require a new tag, copied from |
9bc0bfd
to
902cd13
Compare
I've updated the PR. I've changed the implementation and introduced a new
I've implemented the TagStatement such that it doesn't render anything to the output and only import macros to the local scope. This is a bit opinionated, but I think the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not expecting anybody to be able to do this without asking more questions or saying "it's too complicated", good job!
Give me some time to look again into Jinja and if their way for naming macros has advantages. I don't think it would be more complex than what you implemented, just checking the user cases and how simple the usage is.
@@ -934,7 +934,18 @@ Now `field` is available as a local property of the template and can be invoked | |||
{{ field('pass', type='password') }} | |||
``` | |||
|
|||
> Macros need to be defined before they are used as they are discovered as the template is executed. They can also be defined in external templates and imported using the `{% include %}` tag. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is not changing the behavior wrt includes, so this will still work, right? Not saying this should be removed. I also remember that include
is not recommended in Liquid (because like in this case it leaks to the caller).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is not changing the behavior wrt includes, so this will still work, right?
Yes behavior of includes isn't changed by this PR. But I would still say that include can't be used to add external macros to the local scope. The following example won't work:
functions.liquid
{%- macro hello_world() -%}
Hello world!
[%- endmacro -%}
template.liquid
{%- include 'functions' %}
{{ hello_world() }}
This will not render Hello world!
Actually the opposite will work.
You can access a marco declared in a template from the included file.
Hope this answers your question.
Are you talking about not rendering the imported template? Is so then yes definitely should not render, just import functions from it. |
Done with checking Jinja again. Can you confirm that what you did is compliant with Jinja's syntax, but you just don't handle the Examples:
|
The from tag will import macros defined in a separate file.
1167f05
to
c3df8ef
Compare
Fix for #551
Macros included using the
include
statement are always undefined because they are defined within the scope of theinclude
statement. To prevent this, this pull-request changes the implementation of themacro
statement to always register the macro to the root scope.