Adding HTMX to GO
HTMX is the successor to intercooler.js, as is used to extend HTML with HTTP commands without needing to write an API. Now, I know in the beginning I said I was removing abstraction layers, however I'm more of systems/tooling programmer, so I still need some abstractions until I have a grasp of what is actually happening underneath.
Basic Concept
HTMX deploys AJAX commands to modify an element. This is similar to how ReactJs works. ReactJs allows for updating content, and HTMX is fulfilling that for HTML.
Import HTMX
A simple one liner is added to ./internal/views/layout.templ
element.This was already included in the repo, however it has been updated to verify the script.
Using HTMX
HTMX comes with all of your favorite keywords appended with hx-.
# General format is hx-[verb] hx-get # HTTP GET hx-post # HTTP POST hx-put # HTTP PUT hx-patch # HTTP PATCH hx-delete # HTTP DELETE hx-swap # update content of an element hx-target # specify element to affect hx-trigger # action that executes function
There are more, however these are the main ones used in this project.
For a simple test, in ./internal/views/components/logo.templ, inside of the opening tag, we're going to add hx-get="/" and hx-trigger="click".
Open your terminal and run:
templ generate go run ./cmd/server/main.go
Now go to your browser and go to localhost:[YOUR PORT]/. Click on Gopher, and you should see... well, it happened so fast, you probably didn't notice. That's okay. Open the developer tools, and go to the inspector tab. Click the Gopher again. You should notice the update in the HTML in the inspector tab.
HX-SWAP
This is the bread and butter of HTMX. This is what gives us the responsive UI/UX we're looking for. Now, hx-swap, while simple in name, needs careful consideration on it's location. By this, I mean, do not put it where it will interfere with other elements.
Example:
<div > // container <button hx-delete="[endpoint]" hx-target="nearest [element]" hx-swap="outerHTML" hx-get="[endpoint]" hx-target="this"> // actor </button> // end actor </div // end-container
Placing all of the control on the button, will cause everything to be erased and prevent a button, for updating, being displayed. However, if we move some of the work to the container:
<div hx-get="[endpoint]" hx-target="this"> // container <li> <button hx-delete="[endpoint]" hx-target="nearest [element]" hx-swap="outerHTML"> // actor </button> // end actor </li> </div // end-container
Now, when we click the button, only the data INSIDE of the container is changed, except now a button exists for further editing.
Addendum
I'm stopping here for two (2) reasons.
First, you can use htmx and customize your site with it as it is. Second, we can return html code with a http.Reponse. By extension, we can pass templ components as well. Do you see where this is going?
Coming Soon
An entire restructure and moving functionality into go handlerFunc()s.
The above is the detailed content of Adding HTMX to GO. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...
