Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Can we forcibly render the partial as the entire page? #47

Closed
mekhami opened this issue Mar 1, 2021 · 1 comment
Closed

Comments

@mekhami
Copy link

mekhami commented Mar 1, 2021

Okay, the title is a bit strange, bear with me for a second while I explain.

When I was figuring out how htmx and django played together, I stumbled upon the fact that if you you hx-push-url="true" and that url is a view with a partial template (such as would be used for hx-get), it rendered only that partial. At first I thought, "oh, right. that won't work." So your solution here with checking if a request is request.htmx or not solves that problem nicely.

But then I realized... it could actually be extremely useful for development and testing. When I rendered just that one form I was testing, outside of the context of the rest of the page, it was... really nice. I could hone in on just the component that I was testing, coming from that view. In the case of a modal, the rest of the page is pretty irrelevant when I want to test and develop just that modal in isolation.

So what I started doing was, I had a base.html template that looked like this:

<!DOCTYPE html>
<html>
  <head>
    <title>Stocker!</title>
    <script src="https://unpkg.com/htmx.org@1.2.1" integrity="sha384-t37OW0DbqgL50XZW+Tl8TLchxsPoxIJ7NQ/l4aSZIXZVJQAhyQvvkWZgL/SiY/jD" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/hyperscript.org@0.0.4"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
  </head>
  {% block base %}
  {% endblock %}
</html>

and a base_body.html template that actually contained my 'base layout', my navbar, my structural stuff.

My "pages", like the home page, extend base_body.html and are fully part of the layout.

My "components", like a modal, extend "base.html" so that they get the relevant scripts and css etc, but are rendered in isolation.

So my thought is, can I have both this "render just this component for testing and debugging and stuff" request path, and the dual "partial vs complete" path as demonstrated in your demo? Is that feasible? (More importantly, do I really want it? When I demo it to colleagues, they think it's potentially a really cool feature, but maybe it's silly.) Could there be some sort of DEBUG related flag that allows you to render the partial in a request or in a test?

Thanks for hearing my rambling. Hope some of that made sense.

@adamchainz
Copy link
Owner

Do you mean something like this?

    if request.htmx:
        base_template = "_partial.html"
    elif "minimal" in request.GET:
        base_template = "_minmal.html"
    else:
        base_template = "_base.html"

Where _minimal.html is the minimal head template you described above? Then you could visit /something?minimal to see the page without the layout.

If so, this is not even anything to do with htmx, it's just using Django templates ;)

Closing assuming this answers your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants