carltongibson / django-template-partials

Reusable named inline partials for the Django Template Language.
MIT License
387 stars 15 forks source link

partial with context #13

Closed jmuncaster closed 10 months ago

jmuncaster commented 1 year ago

Hi, Thanks for a neat little library. I am asking about a feature that I was expecting to see but which is not there, and was wondering if it is intended to be implemented or is out-of-scope.

I was expecting to define partials essentially as mini reusable blocks in a page.

{% startpartial my-card %}
<div>
  <h1>{{ title }}</h1>
  <p>{{ content }}</p>
</div>
{% endpartial %}

...

{% partial my-card with title="Title 1" content="Content 1" %}
{% partial my-card with title="Title 2" content="Content 2" %}

(I think these are sometimes called "components")

Is something like this planned for this project?

carltongibson commented 1 year ago

Django already has the with tag. Would that not serve? (That's how I'm using it myself.)

So...

{% with title="Title 1" content="Content 1" %}
{% partial my-card %}
{% end with %}

The include tag does have the with option like you describe. (I have to admit to never having used that, rather favouring the with tag...) so it's not 100% unlikely.

In general I'd favour keeping the API surface area as small as possible.

jmuncaster commented 12 months ago

Got it, thanks for the recommendation! I guess my mental model here is that of a function call, so it feels odd to me to have to list the argument names in a with block before "calling the function". Under this mental model I think including the with statement in the call to the partial is better w.r.t. encapsulation. But perhaps I'm not thinking of it right.

carltongibson commented 10 months ago

I'm going to close this as out-of-scope for here.

The goal here is the named partials bit, with the aim of getting that into the DTL, hopefully over the Django 5.x cycle.

Use the with tag if you need it.