-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
Reusable macros #40
Comments
Sounds like an idea, thanks for your suggestion. I will think about it. |
or
Please add as or = statement |
Or even load all macros from specified file (without any names) |
Yeah I find this takes macros to a whole new level. In current state its not much use for me, but once they can be reused across from multiple templates it'll become very useful for me. |
Yes, it'll make its way into pongo2 somehow. Will have some time at the end of next week to think about it deeper. |
One difference from jinja2 I noticed is that in jinja2 you can use keyword args when calling macros (pongo2 lets you define the macros with keyword args). Would that be hard to implement? |
It would require some fundamental changes in the variable resolver of pongo2 and would lead to a more complex (and time consuming) resolving process. Not sure whether it's worth it. |
As usual, brilliant explanation, thanks for this. |
So this feature will not be implemented? |
I have plans to implement macro imports in pongo2, my last answer was w.r.t. the last question. |
I implement the following syntax for macro imports:
Will be available soon. You can't override macros (specifically the macro name) by providing another key through the Context (will lead to an error when calling one of the |
Sounds good, can't wait to test it. |
Please test it. See the commit message on how to use it and what changed. Let me know if it works for you. |
Brilliant, only found one issue when calling another macros from the macro. I got a macro file _form.html:
I've tried moving the import statement into the macro itself and that blows things up. |
Which error message do you get and what does the calling chain looks like (which macro calls which macro from which file)? |
Try moving the import statement into the macro (nothing else gets evaluated except for the macro-block when importing a macro). |
macros/form.html
user/_form.html
This gives bunch of errors such as:
It seems like the import statement inside the macro is executing the macros. |
No, that's what my example here is doing, and gives bunch of errors. |
Can you please show me the error when you're not importing the macros inside the macro as well (like in your first post)? Thanks! |
In that case there are no errors, pongo2 doesn't find the macros (render_input, render_input_csrf and submit_csrf) and doesn't execute them. Only thing my macro then yields is the form, fieldset and the select box. |
Okay, thanks for your feedback. I have to investigate that and will create a new issue for it. |
Thanks, let me know if you need anything from me :) |
@fromYukki Same here: Would be cool if you could give me a short feedback whether this works for you as expected. |
{% import "my-macros.tpl" my_macro, my_other_macro as renamed_macro %}. A macro can be exported by adding "export" to the declaration: {% macro my_macro() export %} ... {% endmacro %} Exported macros can't be overriden by a user's Context. Introduced new API-function: AsSafeValue. Introduced new keyword: "export" Conflicts: tags_macro.go
in jinja2 you can do something like this:
Would it make sense to implement so macros can be imported and reused instead of them having to be in the same template file as they're used in?
The text was updated successfully, but these errors were encountered: