-
-
Notifications
You must be signed in to change notification settings - Fork 223
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
Children like support for macros / included templates #1077
Comments
I think the standard approach would be to use template inheritance? (To be clear, you can have multiple levels of inheritance IIRC.) (Sorry for the slow response.) |
I have no idea how can this be done with template inheritance. I am not able to use Maybe it is not clicking to me how can I use this This all applies if I understand correctly how this works. Either way, this doesn't help make the code modular. More the other way around. |
I guess I don't understand your use case very well. Are you aware, too, that templates implement |
I am aware of that. From my workaround example it should be easy to understand that I am just creating a reusable wrapper component. I want to allow parent template to be the one that sends the content into the child template. I want to do this within the templates themselves. Another example could be this: {% macro social_card(svg, heading, content) %}
<section class="border rounded-md bg-pink-200 m-4 p-4">
<header class="flex text-center justify-center items-center gap-2 mb-2">
<svg aria-hidden="true" class="h-7 w-7 fill-blue-950">
<use xlink:href="/svg/icons-sprite.svg#{{svg}}" />
</svg>
<h3 class="text-lg font-medium mb-1">{{heading|safe}}</h3>
</header>
{{content}}
</section>
{% endmacro %}
{% call sc::social_card(
"profile",
"Beautiful header content goes here",
"<span> Here I have total freedom unrelated to the rest of the <code>social_card</code> component </span>"
) |
I think this is something I would use nested template types for, that seems to map well to "reusable wrapper component"? |
I think I'm struggling with a similar issue and Im not sure how to solve it. I want to create a "Card" component (Similar to https://ui.shadcn.com/docs/components/card), so in the simplest case just a div with some styling that has a child component (and in more complex cases maybe it has multiple blocks). An example page I want to create is 2 cards next to each other with different children (e.g. one with a table in it and one with some text) Ive tried a few things: I tried to use a macro, that works great until I need the child to be more than just text (some html elements). Im not sure if/how I can pass html as an argument to a macro. I tried to use inheritance. I created a card component with a content block. I could then create components which extended that card (e.g. a text card and a table card) but it wasn't clear how to avoid creating lots of files in this case. e.g. If I have: // card.html
<div
class="p-6 bg-white rounded-lg shadow-md dark:bg-slate-600 dark:border-gray-700 dark:text-white"
>
{% block content %}{% endblock %}
</div> I can easily extend this to create a fancy stat card for example:
But then if I want 3 of these card in index.html how would I add them? I could create 3 files, one for each card and then But it would be nice if I could have a syntax like the macro and do
but it doesn't seem possible to create a macro which contains an extends within it? |
So I did some research and I'd like to open this as a discussion because I haven't been able to find a good solution to this convention.
Problem
Just like in many other templating languages, I'd like to create a wrapper component template that can be re-used with different content inside the template.
This is in some frameworks referred to as
<slot/>
orchildren
in react.With Jinja this pattern is possible with the
caller
functionality with macros: https://jinja.palletsprojects.com/en/3.0.x/templates/#callWorkaround Example
My current solution is to create 2 macros and put the content in between.
This can however cause a few problems when used incorrectly.
Resolution
I've seen that the support for
caller
was multiple times declined: #996 #930This brings me to a question: How should I approach creating and using templates that are meant to wrap around different content?
The text was updated successfully, but these errors were encountered: