Tag Archives: htmx

Updating UI after a successful HTMX request

  • 👎🏾Reload the page. Send response header HX-Refersh which ensures that page is reloaded afters successful request. This is not the greatest of options because refreshing page causes UI to move.
        /**
        * Add HX-Refresh header to response. The client will refresh the page.
        */
        private function addRefreshHeader(): void
        {
            $this->response->setHeader('HX-Refresh', "true");
        }
    
    
  • 👍🏾 Trigger another HTMX read request that fetches the content on success. If you are coming from react/vue, this will feel natural. Send a header like HX-Trigger: HX-Update-My-Table and setup a hx-get to be triggered on this event e.g.
    <h2>My Table</h2>
       ...
      <tbody id="mytable-table" hx-get="/read/my-table" hx-trigger="HX-Update-My-Table from:body">
        ...
      </tbody>
    </table>
    
    <form hx-put="/put/my-table">
      <label>
        Name
            <input name="key" type="text">  
      </label>
      <label>
        Email
            <input name="value" type="text">  
      </label>
    </form>
    
  • There are more interesting options https://htmx.org/examples/update-other-content/

November 29, 2025: Weekly Notes 2025/24

  • 🏃🏾124 km more to go to finish 1000 km run this year. Pretty doable in 32 days!
  • 👨🏾💻I am increasing my use of HTMX. A few things I loved about HTMX
    • I can use server to manage my app’s state. The client does not have to do any state management which is a huge win.
      • I don’t have to use TS/JS everywhere, especially on servers. I can write as little JS as possible.
    • I don’t have to update client when I update my app. Decoupling is easy to achieve!
  • 🏏I gleefully watched South Africa trashing India 2-0 in home test series. Except for Jadeja, not a single player showed any fight or even some grit.
    • Also not giving Bumrah captaincy when Gill was out of the series reinforces the accusation that bowlers are a lower caste in Indian cricket. Even the great spinner Ashwin has to pose with a bat for this autobiography. For the f**k sake!!

  • 📺Plur1bus is an interesting series. Well done!

Using $\sout{\text{AJAX}}$ HTMX with Laravel + Blade

I’ve a list of uploaded files that I plan to render in a view. For each file, I want to call an API endpoint e.g. /api/v1/fileinfo to fetch some information and display it.

c7056b55f49a4c00aa56b6a26fe19f5e -- List of files in blade template

My blade template is following. A sample HTML page generated from it is shown above.

@extends('layouts.app')                                                                                                                 

@section('content')                                                                                                                     
    <div class="container">                                                                                                             
        <h3>Files</h3>                                                                                                                  

        @foreach ($files as $file)                                                                                                      
            <div class="card m-1 px-2 py-1">                                                                                            
                <div class="card-content">                                                                                              
                    {{ $file['display_name'] }}                                                                                         
                </div>                                                                                                                  
                <div>                                                                                                                   
                    <button class="btn btn-secondary">Show Information</button>                                                         
                </div>                                                                                                                  
            </div>                                                                                                                      
        @endforeach                                                                                                                     

    </div>                                                                                                                              
@endsection

Being old school, I don’t want to enable livewire/inertia just for this. I thought of of using AJAX which all the cool kids were using a decade ago.

Enter AJAX HTMX

Recently I read about HTMX. It is a good opportunity to play with it. The HTMX documentation (</> htmx ~ Documentation) is superb. The API looks great. AJAX can wait.

In code sample shown below, htmx-post define the endpoint that will be called with data defined in hx-vals when hx-trigger event occurs. The inner img with class htmx-indicator will get triggered and we’ll see a indicator spinning for a short while. The value received from server will be put into hx-target which has id $id. $id is generated randomly by PHP to create one-to-one mapping between hx-target and target div.

        @foreach ($files as $file)                                                                                                          
            <div class="card m-1 px-2 py-1">                                                                                                
                <!-- Generate a unique id to insert content received from POST                                                          
                    request -->                                                                                                             
                @php($id=uniqid("div_"))                                                                                                    
                <div class="card-content">                                                                                                  
                    {{ $file['display_name'] }}                                                                                             
                </div>                                                                                                                      
                <div>                                                                                                                       
                    <button class="btn btn-link"                                                                                            
                        hx-post="/api/v1/fileinfo"                                                                                          
                        hx-vals='{"path" : "{{ $file["path"] }}" }'                                                                         
                        hx-target='#{{ $id }}'                                                                                              
                        hx-trigger="mouseenter"                                                                                             
                        >                                                                                                                   
                        File Information                                                                                                    
                        <img class="bg-primary htmx-indicator" src="{{ asset('svg/90-ring.svg') }}" />                                      
                    </button>                                                                                                               
                    <div id="{{ $id }}">                                                                                                    
                    </div>                                                                                                                  
                </div>                                                                                                                      
            </div>                                                                                                                          
        @endforeach

Let’s see this in action. We barely see the spinner since the request doesn’t take much time.

00694da521964fe6b9c9edaf99485ca7 -- HTMX in Laravel Blade

Importantly, note that we render raw JSON😢. HTMX expects HTML from servers and doesn’t support JSON → HTML conversion natively. Well, this sucks for obvious reasons. Who returns HTML from APIs?!

So we have to turn JSON into HTML by ourselves. Thankfully there are community maintained plugins such as client-side-templates. See https://github.com/bigskysoftware/htmx-extensions/blob/main/src/client-side-templates/README.md. The plugin documentation is pretty clear. I am going to show the final solution.

  • The top-level div has hx-ext set that enables the extension.
  • hx-target is replaced by mustache-template.
  • 👉🏾 Our <template> is added. If you are using blade then you have to prefix mustache template with @ so that blade doesn’t touch them. The template create HTML out of JSON.

04c2c39271db4853b35822b149e30634 -- Code Snippet showing how to use htmx

Let’s try again! Great, I got most basic functionalities working.

05047d703e9b446d823bc8e4bff79096 -- Demo: HTMX with blade template

Apparently HTMX is quite powerful. You can search HN for resources https://hn.algolia.com/?q=htmx