So that's what links, from a hypermedia mechanic standpoint, that's what links do. So what HTML, or excuse me, what HTMX does is it generalizes each of these ideas. So there's an event here. I clicked on this thing. So HTMX generalizes that and says, okay, any event can trigger an HTTP request. HTMX also makes pretty much any element in HTML a potential hypermedia control. So default HTML, all you have is links and forms. With HTMX, now anything can be a hypermedia control. It gives you control over the form of the request, whether or not it's a get or a put post, but it also gives you access to put and a bunch of other ones as well. And then perhaps most importantly, this link, when you click on links or you submit forms in traditional web apps, you've got to replace the entire viewport with whatever comes back. So I do have to say, you can use iframes to do partial replacements, but iframes suffer from their own issues.
So what HTMX does is it says, okay, let's generalize that so that when the request is made and the HTML comes back, let's make it so that we can put that HTML anywhere on the page rather than having to replace the whole viewport. So let's go through the attributes in HTMX that let you do this stuff. So here's a like button that's been done in HTMX, and let's just go through the attributes on it that are being used by the author using HTMX to make this like button work. The first thing to note is this hxput. And so HTMX has a series of attributes, hxget, post, put, delete, and patch, that allow you to effectively say on this thing, on this, whatever this element is, when the triggering event occurs, and we'll talk about that in a second, when a triggering event occurs, issue this type of request. So now any element in your HTML document can effectively become a hypermedia control. It can issue a hypermedia request to a server and get back HTML. That's the first part. The second part is hxtrigger, and this is what generalizes that event. So with links, you have the event as a click. When you have forms, the event is a submit, and so with HTMX, you can use any event, so mouseover or whatever. Here we're using click, which is pretty obvious for a button. In this case, you could actually omit this, because of the default trigger for a button is going to be click, but it's here just for, you know, so you can see this sort of explicitly. So we have this ability to sort of trigger an HTTP request on anything, not just clicks and submits on links and forms respectively.
And then the last real big step for generalization here is this hxtarget, and what this says is after you issue this put to this URL and you get back some HTML, what I want you to do is I want you to take the response content, and I want you to put that into the thing with the ID output. This is using a CSS selector, so this is a standard CSS selector that says the thing with the ID, that's what this hash means, output. And below, we have an output tag with, sure enough, an ID on it called output, and so what's going to happen is when a user clicks on this button, it's going to issue a put to like on the click, and then it's going to get some HTML content back, and that content's going to be inserted inside of this output. And this turns out to be a really important generalization of HTML. It really opens up what you can achieve with HTML.
Comments