carltongibson / django-template-partials

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

Inline partial definitions? #7

Closed ghickman closed 10 months ago

ghickman commented 1 year ago

Sometimes it’s useful to have an existing section of a page re-used in another view. The below example is a boiled down example from a real life app. It’s a page with a form, where another page displays just the form in a modal with HTMX.

{% extends "base.html" %}

{% load partials %}

{% startpartial form %}
<form method="POST" hx-post="{{ post_url }}">
  {% csrf_token %}

  # form fields go here

  <button class="btn btn-success" type="submit">Create</button>
</form>
{% endpartial %}

{% block content %}
# some content goes here

{% partial form %}

# some more content goes here

{% endblock content %}

Defining the partial at the top of the full page template works, and referencing that partial with the # syntax in an HTMX view is already a big improvement. But I wonder if the partial template tag could be used inline in the main template too? It feels like it fits with Locality of Behaviour, however the project examples suggest the design was tailored more to reuse of template sections (one could call those components!), so I’m unsure if supporting inline definitions is desired here. What do you think?

Apologies if there is a better place to raise this and thank you for pushing forward this idea in the community!

carltongibson commented 1 year ago

Hey @ghickman — good question!

I specifically had partials defined at the top, for two reasons:

  1. I needed re-use.
  2. I like the way extracting it makes the main template easier to read. (I have a quite complex table in play, and breaking it up makes it a lot easier to navigate.)

So I don't want to get rid of the current behaviour

Nonetheless, I guess because folks have been using render-block, the idea of having them inline has come up a couple of times now.

So… 🤔

DefinePartialNode, that's the result of a {% startpartial ... %}{%endpartial %} pair, doesn't output it's content directly;

https://github.com/carltongibson/django-template-partials/blob/67061e28fe9a0fbb37e320f36698c82c68ab39f5/src/template_partials/templatetags/partials.py#L31-L38

Perhaps it could though with an added inline argument... {% startpartial partial-name inline=True %} (or similar)

Would you fancy investigating that? (Any inline should default to False. Declaring a partial outside of a block generally means it wouldn't be output anyway, but where you're not using extends it would be, so I'd want it to be opt-in)

ghickman commented 1 year ago

specifically had partials defined at the top, for two reasons

Both make good sense!

How do you feel about the changes I've proposed in #9?

I've just re-read the very end of your comment and need to test out my changes in that context, but do you think I'm on the right path in terms of the tag's API?

carltongibson commented 1 year ago

Hey @ghickman. First pass looks good yes 👍 Please fiddle until you're happy and then let me know.

🏎️

carltongibson commented 1 year ago

@ghickman Did you want to look any further at this, or is it ready to go from your PoV? (No sure from the conversation…)

Thanks!

ghickman commented 1 year ago

Hi @carltongibson, sorry for the slow reply, life&work got in the way!

I think this is ready for a review now please. I've got a local test project where I've tested the various scenarios out to confirm to myself it's doing what you expected, so hopefully it covers all your use cases too 🤞

carltongibson commented 1 year ago

Great. Thanks @ghickman!

I'm just waiting for a follow up on #11 and then I'm going to bundle up a new version.

carltongibson commented 10 months ago

Fixed by https://github.com/carltongibson/django-template-partials/pull/9. New version in the next week or so.