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

support jinja-style macros #127

Open
abelnation opened this issue Aug 10, 2016 · 5 comments
Open

support jinja-style macros #127

abelnation opened this issue Aug 10, 2016 · 5 comments

Comments

@abelnation
Copy link

docs:
http://jinja.pocoo.org/docs/dev/templates/#macros

@yogthos
Copy link
Owner

yogthos commented Aug 10, 2016

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.

@boxxxie
Copy link
Contributor

boxxxie commented Jul 7, 2022

link is broken
https://jinja.palletsprojects.com/en/3.1.x/templates/#macros

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 {% include "my_macro_file.jinja" %} in your files?

@brettatoms
Copy link

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.

@boxxxie
Copy link
Contributor

boxxxie commented Oct 5, 2022

icon in this case is a macro

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)

{%- i have seen this syntax before, but i couldn't find a definition for it in the jinja docs

how do {% import and {% extends interact ?

what is the difference between {% import and {% include ? other than aliasing

@brettatoms
Copy link

brettatoms commented Oct 6, 2022

icon in this case is a macro

Yes icon is a macro. So in this example we import a macro from another file and also define and use a macro (sidebar_item) that calls the icon macro.

{%- i have seen this syntax before, but i couldn't find a definition for it in the jinja docs

The {%- tag strips whitespace: https://jinja.palletsprojects.com/en/3.1.x/templates/#whitespace-control

how do {% import and {% extends interact ?

They're mutually exclusive. You can only extend one other template but you can import multiple other templates.

what is the difference between {% import and {% include ? other than aliasing

The difference has to do with passing the current context. See https://jinja.palletsprojects.com/en/3.1.x/templates/#import-context-behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants