carltongibson / django-template-partials

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

Understanding how to use django-template-partials #28

Open Yacobolo opened 1 year ago

Yacobolo commented 1 year ago

Hi Carlton, just a heads up - i am new to django and htmx and had a hard time understanding how to use your library. I learn best using a youtube video and after seeing this from bugbytes i all made sense: https://www.youtube.com/watch?v=fij85rIPoCw Maybe this could be referenced in the docs?

This is just my personal experience. Thansk for making an awesome package!

carltongibson commented 11 months ago

Hi @Yacobolo - thanks for the idea.

I'm kind of hoping the package will just sort of disappear into Django but let me ponder 🤔

HamaBarhamou commented 11 months ago

yes, I admit I didn't quite understand it either. I think a lot of work needs to be done to make it easier for beginners to understand django and html.

GabeSoler commented 4 months ago

Hi, I am also new to Django and quite confused about his it works. I have also been checking Django unicorn, which has a components way, and has more info on how it works. I like more the idea of partials because it feels more contained and uses the same views system; but I am still not familiar with htmx. Is the whole partials htmx dependent? I imagined I could submit a form like always but will be partially contained, and that sounded awesome. But now I see I would need to add a complete set of things to the input and that does not sound so cool(mostly because I have been working with bootstrap that makes the form on its own). If there would be more examples it would be great. Gabriel

jillesme commented 2 months ago

The documentation could use a little clarifying update! It took me a little while to understand what this package actually does. It's not very clear what this package does from just:

{% load partials %}

{% partialdef test-partial %}
TEST-PARTIAL-CONTENT
{% endpartialdef %}

and

With the partial defined, you can reuse it multiple times later:

{% block main %}
BEGINNING
{% partial test-partial %}
MIDDLE
{% partial test-partial %}
END
{% endblock main %}
The partial content will be rendered in each time the named partial is used.

I'll see if I can make a documentation PR later. It seems there are 2 major ways to use template partials. Mostly in combination with hypermedia but not exclusively.

1. As top level "components". Where you can get the partials by using template#partial. e.g. file.html#user-heading.

# file.html
{% load partials %}

{% partialdef user-heading %}
<h1>Hello {{ user }}</h1>
{% endpartialdef %}
<p>My content</p>
# file2.html
# note: does not need {% load partials %}

<div>
    {% include 'file.html#user-heading %}
</div>

2. As inline partials. This is very useful for htmx

{% load partials %}

<form hx-post="{% url 'your-view' pk %}" hx-trigger="submit">
{% partialdef form inline %}
{{ form.as_p }}
{% endpartialdef %}
</form>
def your_view(request):
    form = YourForm(request.POST)
    if form.is_valid():
        # do something

    return render(request, 'file2.html#form', { 'form': form' }

I think you'd want this library to separate from the HTMX package @carltongibson but I also believe the majority of people who want to use this package, use it in combination with hypermedia. Perhaps the documentation could reflect that.

Did I miss anything and WDYT?

carltongibson commented 2 months ago

Hi @jillesme, the include usage is cool and all but surely secondary to intra-file {% partial ... %} usage no? That's certainly my primary use-case.

jillesme commented 2 months ago

Hi @carltongibson I suppose I did miss that prime use-case since I haven't used it like that yet! I've mostly used it for HTMX (and loving it). Could you give an example of how you use it mainly intra-file?

Are you defining template-partials component-esque?

carltongibson commented 2 months ago

I don't quite what you intend by "component" but using {% partial %} is much like the {% include %} case, except it's got better Locality of Behaviour, since the partial lives in the same file — and I'd suggest it's primary, as — at least how I use it — I'll define a partial for use in the same template before I'll include the same in a separate file.

jillesme commented 2 months ago

I see what you're saying now. I think the documentation would be served well by explaining that use case with a more concrete example.

In the example of a book list website, you'd define a partial for a book entry in the list.html page, and then use that partial immediately in the same template. Instead of doing {% include "partials/book-item.html" %}. To increase LoB.

Are we on the same page here or have I misunderstood your primary use case

carltongibson commented 2 months ago

That sounds about right, yes.

And then the view case is currently served by the template_name example.

Granted both could be expanded (slightly)