Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Children like support for macros / included templates #1077

@michalvankodev

Description

@michalvankodev

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/> or children in react.

With Jinja this pattern is possible with the caller functionality with macros: https://jinja.palletsprojects.com/en/3.0.x/templates/#call

Workaround 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.

  {% call sc::social_card_start("profile", "Beautiful header content goes here") %}
    <span> Here I have total freedom unrelated to the rest of the <code>social_card</code> component </span>
  {% call sc::social_card_end() %}
{% macro social_card_start(svg, heading) %}

<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>

{% endmacro %}

{% macro social_card_end() %}
	</section>
</section>
{% endmacro %}

Resolution

I've seen that the support for caller was multiple times declined: #996 #930

This brings me to a question: How should I approach creating and using templates that are meant to wrap around different content?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions