-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
support jinja-style macros #127
Comments
I'm open to a pr for this, but I likely wouldn't have a chance to work on adding macros in the near future. |
link is broken based on wikipedia, this seems to be the official site. i've been doing meta templating with selmer recently, and this sorta looks like it could be useful for me. i don't really understand how people use these with jinja, though. is there some magic file that gets auto included in all your templates that has macros? or are you supposed to do something like |
Jinja-style macros are really helpful for reducing markup that gets repeated over and over. This is how I use them: <!-- The icons.html file contains macros that return the markup for SVG icons -->
{% import "icons.html" as icon %}
{% macro sidebar_item(title, href, icon) -%}
<li>
<a href="{{ href }}"
class="text-gray-600 hover:bg-gray-50 hover:text-gray-900 group flex items-center px-2 py-2 text-base font-medium rounded-md"
>
<div>{{ icon }}</div>
{{ title }}
</a>
</li>
{%- endmacro %}
<ul>
{{ sidebar_item("Home", "/home", icon.home() }}
{{ sidebar_item("Users", "/users", icon.users() }}
{{ sidebar_item("Organizations", "/organizations", icon.someicon() }}
</ul> IMO this would be a killer feature for Selmer. |
i'm already doing something pretty similar to this on the app i'm working on, which does a lot of meta-templating (templates that return templates)
how do what is the difference between |
Yes
The
They're mutually exclusive. You can only extend one other template but you can import multiple other templates.
The difference has to do with passing the current context. See https://jinja.palletsprojects.com/en/3.1.x/templates/#import-context-behavior |
docs:
http://jinja.pocoo.org/docs/dev/templates/#macros
The text was updated successfully, but these errors were encountered: