https://htmx.org logo
Join Discord
Powered by
# htmx-general
  • w

    wonderful-school-81222

    12/12/2022, 10:22 PM
    Hey hey — Yep. I figured that part out. I guess I'm trying to understand how to avoid redundant code in all my methods that look something like:
    Copy code
    python
    if hx_request:
      return templates.TemplateResponse("account.html")
    else:
      return RedirectResponse("/")
  • m

    mysterious-toddler-20573

    12/12/2022, 10:25 PM
    I had a filter in rails that would determine whether or not to render the chrome (the layout) for a given request based on the presence of
    HX-Request
    and then I created a helper function for more elaborate situations where I wanted to select a particular template based on the exact path/target/etc
  • m

    mysterious-toddler-20573

    12/12/2022, 10:25 PM
    not sure how that maps to your server side setup
  • w

    wonderful-school-81222

    12/12/2022, 10:27 PM
    That makes sense to me. I would imagine that I could either: 1) swizzle some Python method that would pre-flight each request and perform operations 2) Use the FastAPI middleware API https://fastapi.tiangolo.com/tutorial/middleware/#middleware
  • o

    orange-umbrella-16693

    12/12/2022, 10:28 PM
    https://discord.com/channels/725789699527933952/1046573806547910677/1051982486822993981 Isn't websockets more widely supported than SSE?
  • w

    wonderful-school-81222

    12/12/2022, 10:28 PM
    Then I'd just need to find out how to see that the user is deep linking to /accounts/1235 and then compose the appropriate templates if I don't see HX-Request
  • w

    wonderful-school-81222

    12/12/2022, 10:43 PM
    Hmm, how about if I solve this with a conditional Jinja2 extends statement?
    Copy code
    html
    {% extends request.is_hxrequest|yesno:"base_ajax.html,base.html" %}
  • w

    wonderful-school-81222

    12/12/2022, 10:44 PM
    That way accounts.html can have a conditional base template that it extends the base template on the condition of whether the caller is calling with
    HX-Request
    or not.
  • w

    wonderful-school-81222

    12/12/2022, 10:44 PM
    Gonna try it out and blog about it if it works out for me.
  • b

    bumpy-kangaroo-60192

    12/12/2022, 11:02 PM
    I have a mixin base class that checks for hx headers and sets a base_template variable in the context.
  • b

    bumpy-kangaroo-60192

    12/12/2022, 11:03 PM
    Then my HTMX aware templates {% extends base_template %}
  • w

    wonderful-school-81222

    12/12/2022, 11:08 PM
    Yeah. My jinja2 templates are going to have a conditional base template applied.
  • b

    bumpy-kangaroo-60192

    12/12/2022, 11:11 PM
    My site has two ... experiences. Basically the same pages with the same information but on more than one layout, and they're both working off the same HTMX responses. I'm really surprised how easy this is. Definitely more elegant than my javascript was.
  • w

    wonderful-school-81222

    12/12/2022, 11:18 PM
    That makes sense. I'd love to see this discussed in the HTMX page because navigation and the history is the most confusing thing.
  • a

    ancient-shoe-86801

    12/12/2022, 11:25 PM
    this is what I was pointing about libraries that provide helpers like
    request.htmx
    , so you can do
    if request.htmx
  • a

    ancient-shoe-86801

    12/12/2022, 11:25 PM
    they exist for flask and django, not sure about FastAPI
  • w

    wonderful-school-81222

    12/12/2022, 11:25 PM
    Feeding the request header state is trivial. Arriving at the approach to differentially render partials vs "full page" templates seems like a gap in understanding.
  • a

    ancient-shoe-86801

    12/12/2022, 11:26 PM
    oh, yeah, coming for a good reusable pattern for that is an interesting issue
  • a

    ancient-shoe-86801

    12/12/2022, 11:26 PM
    so that you don't litter many of your endpoints logic with if statements
  • a

    ancient-shoe-86801

    12/12/2022, 11:49 PM
    taking you example above, the issue I have found is this: Imagine a page where you have a menu with dropdowns as a toolbar, and then some content inside. You want to load either the full page, or only the content box. You could move that logic to the template, but, consider that to populate the contents of the dropdowns you need to issue some SQL queries, and pass the results to the template. If you are not going to render the dropdowns, doing that is a waste of resources. So you want to carefully control what data you want to pass to the template for each case.
  • m

    mysterious-toddler-20573

    12/12/2022, 11:51 PM
    i often to split sub-resources into their own routes and then, if there is some shared state calculation, pull that out to a function, so you end up w/ a few sub-routes that all call an init function and then render some particular bit of the UI
  • m

    mysterious-toddler-20573

    12/12/2022, 11:52 PM
    minimizes the if statements, your hypermedia API reflects your hypermedia needs
  • m

    mysterious-toddler-20573

    12/12/2022, 11:52 PM
    top level resources do the if htmx request, render partial else render full page logic, usually via some sort of middleware
  • a

    ancient-shoe-86801

    12/12/2022, 11:55 PM
    having routes checking for if they are responding to an htmx request feels like the anti-pattern of adding flags to a function call
  • m

    mysterious-toddler-20573

    12/12/2022, 11:55 PM
    yeah, I don't mind a bit of it, but I try to avoid it in general
  • m

    mysterious-toddler-20573

    12/12/2022, 11:56 PM
    when I do it, I go all in tho
  • m

    mysterious-toddler-20573

    12/12/2022, 11:56 PM
  • a

    ancient-shoe-86801

    12/12/2022, 11:57 PM
    as always, trying to use the best tool for the job. Even so called anti-patterns have their place.
  • r

    refined-waiter-90422

    12/13/2022, 12:42 AM
    In all fairness, you don't /have/ to render a partial, can always just use something like
    hx-select="#page-content"
  • g

    gorgeous-ghost-95789

    12/13/2022, 12:42 AM
    I forget about that one all the time 🙂
1...945946947...1146Latest