carltongibson / django-template-partials

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

Adding "as" argument to {% partial %} #14

Closed danjac closed 11 months ago

danjac commented 11 months ago

I have a use-case where I have a set of pagination links at the top and bottom of the page.

It would be optimal to be able to render the partial into a variable, so it doesn't get evaluated twice, for example:

{% startpartial pagination %}
<div>pagination here</div>
{% endpartial pagination %}

{% partial pagination as pagination_html %}

{{ pagination_html }}
....
{{ pagination_html }}
carltongibson commented 11 months ago

Hi @danjac. Thanks for the report.

I can see why you would want this, but I think it's kind-of orthogonal to django-template-partials…—it's more like you need a caching tag.

See the discussion on Django Trac #6378 and the related there.

danjac commented 11 months ago

Yes I always end up with a "render_as" tag (basically wrapping render_to_string) for this issue, as it's something the core developers don't want to add to {% include %}.

carltongibson commented 11 months ago

For me, a separate capture tag is better than adding as all over the place... πŸ€·β€β™€οΈ

danjac commented 11 months ago

As in django-capture-tag?

carltongibson commented 11 months ago

@danjac I haven't looked at the details there, but if it does what it says, then something like that would be useful IMO. (IIUC the resolution on Django #6378 was along the lines of what exactly is still needed here?, rather than no, never, but (again) πŸ€·β€β™€οΈ )

carltongibson commented 11 months ago

Hi @danjac.

@ghickman pointed me to the fragment tag in Django-slippers, that does this:

https://mitchel.me/slippers/docs/template-tags-filters/#fragment

I need to merge #9 and push a new version (During Sept 🎯) but once that's in place you should be able to do what you're looking for here (at the same time as defining a named partial)

danjac commented 11 months ago

Great, thank you!