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

    blue-toothbrush-6988

    01/20/2023, 11:11 AM
    Yeah no reason xD
  • b

    blue-toothbrush-6988

    01/20/2023, 11:12 AM
    i just wanted to fully submerge in to the htmx, was so focused 😄
  • a

    adventurous-ocean-93733

    01/20/2023, 11:12 AM
    Ha, you could also set
    hx-boost="true"
    if you're not already
  • a

    adventurous-ocean-93733

    01/20/2023, 11:12 AM
    Totally get it "htmx all the things"
  • a

    adventurous-ocean-93733

    01/20/2023, 11:14 AM
    The reason it's doing this is that without a
    hx-target
    it will swap itself with the new content.
  • b

    blue-toothbrush-6988

    01/20/2023, 11:14 AM
    @adventurous-ocean-93733 Just to understand why its still feeding the #search-results? Is it because no target is set?
  • b

    blue-toothbrush-6988

    01/20/2023, 11:14 AM
    @adventurous-ocean-93733 got it ❤️ much thanks!
  • b

    blue-toothbrush-6988

    01/20/2023, 11:16 AM
    I am so loving htmx as i my background is basic->Vb.net->python i have very little understanding of javascript and htmx is just saving my ass 😄
  • p

    purple-easter-43922

    01/20/2023, 12:16 PM
    Hi, everyone! Sorry if this has been asked before, but how should I do login and registration forms in htmx? Currently, my login handler has a "happy path" that signs the user in and returns a redirect (/w the session cookie), and if the credentials are incorrect, it simply returns a
    401: Unauthorized
    with some text content.
  • p

    purple-easter-43922

    01/20/2023, 12:16 PM
    How should I change that? What does htmx expect?
  • a

    adventurous-ocean-93733

    01/20/2023, 12:56 PM
    In case it’s useful there are server side example integrations here: https://htmx.org/server-examples/ I don’t know how many have auth built in. If you’re retaining your login handler then you could keep the same pattern you already have - standard html form post that either redirects on success with header, or redirects to login error.
  • p

    purple-easter-43922

    01/20/2023, 1:54 PM
    Is there a way to perform different actions in htmx depending on the status code? For example, if the login is successful, it would do nothing and let the redirect happen, whereas if it's a 401, it would show a hidden "Incorrect email or password" paragraph or perhaps receive that paragraph as HTML and put it below the form.
  • r

    ripe-crowd-36857

    01/20/2023, 2:01 PM
    hi, first message here but I guess your server should return the form with the error "incorrect email or password" (as in HTML)
  • p

    purple-easter-43922

    01/20/2023, 2:06 PM
    I've considered that. With the templating engine (Askama) I'm currently using, that's a bit difficult, though, since the login template inherits from the base template and renders the whole page. I've asked at the library chat if it's possible to make that inheritance conditional (use it on the first render to respond with a complete page, but only render the form for error responses), but I'm waiting for answers.
  • r

    ripe-crowd-36857

    01/20/2023, 2:07 PM
    htmx sends some headers in the request that your server can use to return partial instead of full page https://htmx.org/reference/#request_headers
  • p

    purple-easter-43922

    01/20/2023, 2:08 PM
    Sure, I mean that I don't know if my templating engine allows for rendering partials when they normally inherit (i.e. they have
    {% extends "base.html" %}
    at the top).
  • a

    adventurous-ocean-93733

    01/20/2023, 2:12 PM
    Yes:
    Copy code
    document.body.addEventListener('htmx:beforeSwap', function(evt) {
        if(evt.detail.xhr.status === 404){
            // alert the user when a 404 occurs (maybe use a nicer mechanism than alert())
            alert("Error: Could Not Find Resource");
        } else if(evt.detail.xhr.status === 422){
            // allow 422 responses to swap as we are using this as a signal that
            // a form was submitted with bad data and want to rerender with the
            // errors
            //
            // set isError to false to avoid error logging in console
            evt.detail.shouldSwap = true;
            evt.detail.isError = false;
        } else if(evt.detail.xhr.status === 418){
            // if the response code 418 (I'm a teapot) is returned, retarget the
            // content of the response to the element with the id `teapot`
            evt.detail.shouldSwap = true;        
            evt.detail.target = htmx.find("#teapot");
        }
    });
    From https://htmx.org/docs/#modifying_swapping_behavior_with_events
  • a

    adventurous-ocean-93733

    01/20/2023, 2:13 PM
    htmx won't swap with 400-level response codes by default, so code like this would turn that on and you could handle it in JS
  • r

    ripe-crowd-36857

    01/20/2023, 2:13 PM
    it seems you're using jinja or nunjucks. your page
    login.html
    could look like this
    Copy code
    {% extends "base.html" %}
    
    <h1>Log in</h1>
    {% include "form" %}
    form.html
    would look
    Copy code
    <form>
      {% if errors %}
        <p>Incorrect password or username</p>
      {% endif %} 
      <input type="text" name="username" />
      <input type="password" name="password" />
      <input type="submit" />
    </form>
    now in your controller, you can check for that
    HX-Request
    request header and then either render
    login.html
    or just
    form.html
  • p

    purple-easter-43922

    01/20/2023, 2:15 PM
    Ah, yeah, I remember something like that. I'm guessing there's no way around writing JavaScript. In that case, would there be an issue if I included that below the HTML partial, inside a "local"
    <script>
    tag? I've gotten used to "local" JavaScript from Svelte, and it would convenient if the JS could live in the partial.
  • p

    purple-easter-43922

    01/20/2023, 2:16 PM
    That's an idea!
  • a

    adventurous-ocean-93733

    01/20/2023, 2:17 PM
    I expect so, it's adding an eventlistener to the body
  • m

    mysterious-toddler-20573

    01/20/2023, 3:24 PM
    I need to make
    value
    a possible value for
    hx-target
    . In fact, perhaps you need to do that.
  • a

    adventurous-ocean-93733

    01/20/2023, 3:24 PM
    That's a good idea. That's a terrible idea.
  • m

    mysterious-toddler-20573

    01/20/2023, 3:24 PM
    many such cases
  • c

    clean-midnight-60710

    01/20/2023, 6:25 PM
    Beginner-ish question: I have a form which can get filled either by the user, by hand so to speak, or by a javascript functions triggered from a different part of the page. I would like an http request to be triggered when certain fields in the form get filled, regardless of how they got filled. I tried using hx-trigger="change changed delay:1200ms" but that doesn't respond to the auto-fill... Is there an hx-trigger I could use that would work here?
  • m

    mysterious-toddler-20573

    01/20/2023, 6:33 PM
    I would look at triggering a change event manually when you update the fields via javascript, to keep things simple
  • f

    freezing-sugar-15438

    01/20/2023, 8:56 PM
    Any idea on how to do something like this with htmx or possibly hyperscript? "When any contenteditable="true" element is ctrl+clicked, make an ajax call, and put the response where the cursor is"
  • m

    mysterious-toddler-20573

    01/20/2023, 8:57 PM
    I would likely use hyperscript for this
  • m

    mysterious-toddler-20573

    01/20/2023, 8:57 PM
    lemme get a basic example going
1...993994995...1146Latest