Htmx – high power tools for HTML
htmx.org
htmx.org
I just released 0.0.4, so htmx is still very young, but it's got a decent test suite: https://htmx.org/test/0.0.4/test/
there is a nice extension mechanism:
and some very rough docs on how to pull off pure HTML animations:
https://htmx.org/examples/animations/
happy to answer questions
I guess the hardest thing is getting the word out, but thankfully oftenwrong has done that for me. :)
A question about this example: https://htmx.org/examples/inline-validation/
This paradigm revolves around sending validation requests to server-side endpoints, is there any reason to put that load on the server rather than validate it on the page prior to submit with JS? Or scalability issues?
Not being antagonistic, genuinely curious because I've not seen inline validation done this way.
I've been playing around with local demo, is it possible through extensions to make a wrapper that can perhaps compose GraphQL query inputs from forms?
I manually copy-pasted the source code into my app, and got:
htmx.min.js:1 TypeError: Cannot set property 'Content-Type' of undefined
at Object.encodeParameters ((index):11)
at htmx.min.js:1
The code for it is:xhr.requestHeaders['Content-Type'] = 'application/json'
But I believe it should be
xhr.setRequestHeader('Content-Type', 'application/json')
When I do this, it seems to work
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequ...
so the tests passed but it doesn't work irl...
good catch! want to submit a pull request fix?
Done!
Also this extension API is pretty straightforward, the element reference and parameters are right in the args so it's an easy mapping to GraphQL query strings from there. Awesome!
I was able to get a minimal version working using just this (with json-enc extension patch):
<input
type="text"
name="query"
hx-ext="json-enc"
hx-post="http://localhost:8080/v1/graphql"
hx-trigger="keyup changed delay:500ms"
hx-target="#search-results"
placeholder="<GRAPHQL QUERY TEXT HERE>"
/>
<div id="search-results"></div>
Pretty coollookin' at you, dutch
See from wikipedia:
>Omdat Katholieke Universiteit Tilburg tot een ongewenste afkorting zou leiden, werd het Katholieke Universiteit Brabant (KUB).
Kut-zooi zeg
Edit: Main concern is whether to start a new project now with intercooler or htmx
I am planning on supporting both intercooler and htmx for the forseeable future. I have a large app written in intercooler and I'm not porting it over any time soon. If I were starting a new project I'd use htmx, but then I know the developer pretty well if there are problems. :)
I'm curious why you are starting over. What is different from intercooler besides no need for jQuery? Why HTMX instead of Intercooler 2.0?
I also wanted to stress that htmx isn't just another javascript library, competing with react and the rest. It is focused on HTML and extending HTML to make it a powerful and complete hypertext. I think that the name htmx captures that idea pretty well.
Btw, I'm currently a very happy user of intercooler. Thanks for the awesome lib.
The fact that intercooler didn't have an extension mechanism meant I had to heap a lot of stuff into the core (e.g. ic-action) that was interesting and useful, but made the code base messy and unfocused.
htmx has an extension mechanism, so you can pull in stuff like this (or write your own extension):
that will take pressure off me to keep adding stuff to htmx and simply add more extensions.
The dependencies idea from intercooler, for example, ended up as this extension:
I was a fan of intercooler and used it in production more than once but for some reason I could never get my head round the dependency functionality and ended up just ignoring it.
Intercooler works beautifully alongside Django and Django Rest Framework and I look forward to trying out htmx.
https://htmx.org/examples/click-to-load/
I see no network traffic when I click the 'load more agents' button. Am I missing something, where are the requests going?
And, based on some of the comments here, it seems like some people may have forgotten how to do anything that doesn't depend on AJAX. ;-)
This way they can create entire products with zero backend code by simply calling 3rd party API.
This is probably also some very nice static blog engine concept to imagine around this.
And some mix with VueJS as well.
There is a slight deal-breaker for me. Much of the functionality revolves around hx-swap'ing i.e. writing the contents of a response as HTML into/around tags. This requires the server-side to return HTML instead of JSON. From the docs:
"Note that when you are using htmx, on the server side you respond with HTML, not JSON. This keeps you firmly within the original web programming model, using Hypertext As The Engine Of Application State without even needing to really understand that concept."
I would love to use this with existing REST backends, but most are only able to return JSON. How does one use this for AJAX without rewriting existing backends?
Towards the end you can put it all together and trim it down, not sure if there's tooling that auto joins some of the CSS as necessary though.
can use one of three attributes named <template-engine>-temlpate (e.g. mustache-template)a lot of folks conflate AJAX w/ JSON apis, and can't easily imagine an endpoint returning partial bits of HTML that have nothing to do with a public JSON API
worse, people have been misled to believe that JSON APIs are REST-ful
we have a lot of work to do
That matches by experience and is kind of ironic, considering that the X in "AJAX" once stood for XML. ;)
I've never actually used a backend that couldn't return arbitrary content types. Those exist?
Any serious framework would be able to handle this without any hiccups.
It wouldn't be the prettiest HTML but you could work with it via CSS and JS.
To migrate to our flashy new Angular V1 application, we added a application/json content-type that instead of returning the template, returned the data that the template used. So we could endpoint-by-endpoint migrate to the SPA we were building.
Was simply a matter of making the data required for the views a bit more advertised internally, so the controllers could instead not render the views when the content-type is json.
For sure. Most of the backends we write only return JSON. If you request HTML or any other content-type, we return nothing. We just don't implement it.
If you write your backend from scratch and only intend for it to be used only with your web front-end, you can make it return bits of HTML (I have several applications in jQuery that work like this -- the value of the response just gets inserted via ("#id").html(val)).
Our current backends are written to support multiple generic consumers of which a web front-end is only one.
Much easier to use client-side templating as suggested by the author.
What other client-side UI frameworks do you recommend? I'm liking what I'm seeing with HTMX due to its small size and simplicity.
https://htmx.org/attributes/hx-swap-oob/
you can also select content by a selector:
https://htmx.org/attributes/hx-select/
and there is infrastructure for general response transformations via the extensions mechanism:
https://htmx.org/extensions/client-side-templates/
See the source code here:
https://unpkg.com/htmx.org@0.0.4/dist/ext/client-side-templa...
Also the IETF asks[2] that you please stop using "X-" to prepend your custom HTTP headers[3]
[1] https://unpoly.com/tutorial [2] https://tools.ietf.org/html/rfc6648 [3] https://htmx.org/reference/#headers
https://github.com/bigskysoftware/intercooler-js/graphs/cont...
https://github.com/unpoly/unpoly/graphs/contributors
I'm happy to change the headers htmx is young enough to get away with it. Would you like to issue a pull request?
As to a PR for the headers – I might just take you up on that!
It's clear that this is not what you intended, but I think from the response from the creator, that was the tone that you accidentally conveyed.
Just a thought for the future :-).
> SHOULD NOT prefix their parameter names with "X-" or similar constructs.
It doesn't spell out exactly what would be considered a "similar construct", but I think "HX-" might count as a similar construct.
There's no good way to implement the feature you want that would satisfy all reasonable interpretations of that RFC, so switching to "HX-" would be a half-measure.
I understood exactly what was meant by X-HX- and I think it's a good design.
Still, HX- is shorter and about as intuitive as X-HX- so I think it might still be the way to go, even though IMO that RFC isn't a good enough reason to change it.
So I think it's better for new projects to drop the "X-".
On the other hand, I think it's reasonable to prepend with "HX-" because these headers apply to functionality that is specifically related to the capabilities of this library (or similar libs). It is highly improbable that this syntax would ever become standardized in either the HTML or HTTP specs, but if it did, the HX in front of headers makes sense given that the tag attributes are also prepended with an HX.
From an entirely stylistic perspective, I also think that shoving an "X-" in front of all your headers is ugly (ugliness can of course be justified by usefulness, but as stated above I don't think that applies here).
But of course these are just my thoughts on the matter, others might like "X-", though I'll admit I don't know why you would.
Their entire reason (Appendix B) is that "well, if the header eventually becomes a standard header, then there will be old apps that will only work with X-, and we'll have to keep the X- version around forever!" That seems like a very weak reason to me. Most custom headers are not going to become standard headers, and those that do, well, apps can update nowadays and we deprecate old web features all the time.
I see a lot of value in distinguishing non-standard headers as a matter of principle. Also, there's a little bit of classic charm to X-, it's almost a shibboleth of HTTP. Removing it feels as wrong to me as removing the :// from the URI protocol and saying "we really just need :/"
thank you!
But In my opinion it gets a lot of things right, specially around form submissions, validation, error handling, modals, history, navigation, passive updates, etc. It is like Turbolinks++.
But them using CoffeScript is the only reason for why I've not contributed to Unpoly even though I've found both bugs and stuff I'd like improved :/
Web App still has its place. But 90% of the web are Web Pages or Interactive Web Page, not Apps. While Every time I point this out there will be someone stating Gmail as an example of Web Apps, but we will soon have Hey.com to prove it doesn't need to be that way.
However browser vendors and standards body ( Which really is just browser vendors ) has far too much interest in making the Web or Web Browser as another OS / Platform. Rather than optimising for the 90% of our current use case.
It's actually web app developers doing the most to push the web in the direction of web apps.
- make something close to an html5 equivalent to asm.js: remove all ambiguous variants, everything we know is slow.
- "ban" all Javascript except some small pre-defined libraries like this.
- make a catchy name (html-core? web-core?) and a validator for it. Call it a standard.
It will be fast in all browsers, maybe really fast in browsers that care to optimize for it.
If it becomes a thing we can create new simpler pure web browsers (as opposed to todays application platforms that we will then start calling old or legacy html :-)
htmx supports animations too, and it has extensions to add further functionality. Of course those projects differ in their functional range, but they share the way they work; by adding attributes to HTML.
Is this an alternative, or is each really its own niche (and you might use both)? Is there a comparison?
Edit: It seems that htmx is almost entirely around AJAX, and Alpine around a) binding element data to objects and b) css & animations (such as hiding a popup). It would make a lot of sense to use them together (and the hx- even complements the x-). Is that correct?
https://htmx.org/extensions/client-side-templates/
any element below it in the DOM can use one of three attributes named <template-engine>-temlpate
the progress bar demo shows how you can implement a UI that would typically be done w/ javascript using pure HTML responses:
I think you're saying that having html returned from the backend "seems questionable" and is a "type of bad programming".
But that can't be right because that's mostly how the web works? Make a request to a server, server returns html.
Is your issue that it's partial "random" content? How does that make it worse? It's a tried and true solution. The hamburger menu in amazon.com does just that.
Or, you could use the client side template feature of htmx which supports rendering JSON into HTML using a templating language: https://htmx.org/extensions/client-side-templates/
No, it isn't. REST implies HATEOAS, and JSON isn't a hypertext.
Calling JSON APIs REST-ful was always a mistake.
http://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.ht...
Why do you think so? It might interest you to know that Roy Fielding defined REST before JSON was invented.
It's just a different way of building web applications, much closer to the original web model. You can do a lot with it, with a lot less complexity in many cases.
I wrote some blog posts about this stuff for intercooler back in the day:
http://intercoolerjs.org/2016/01/18/rescuing-rest.html
http://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.ht...
When a client wants the interactivity of a full-on application, I would use an application framework at that point.
Using a system where the server needs to send back snippets of html for transitory states seems like you'd need to build yourself kind of a framework to handle the mechanics of that on the backend anyway. I'm not sure what the point is then. Sure, there is an asset in your project that is "just html" (the first loaded page), but you're going to need snippets of the interface's various states in different files so that the server can send them back.
It's like this framework is pursuing an aesthetic goal of "just html" which breaks when you actually try to use it for the stuff that you'd use a framework for.
I'm happy you noted the test coverage for the library itself is reaching your standards. That's a great sign.
I think I'd look into a headless chrome setup against my real server if I was doing a large app.
Does it only send data? If yes, I don't really see a clear use-case for it, as you normally either want zero JS and just use plain HTML forms posting or you have a dynamic interface and you use a lot of JS to update data on the page.
but you can use the client-side-templates plugin if you want to run responses through a client-side template engine:
the 'revealed' event logic doesn't use an intersection observer because of IE, that's probably the most annoying thing I can think of
What's the Accessibility story?
The haiku is a nice touch!
I'm immediately turned off by the demo though, because it's relying on a 'fake http server' in the client side JS.
If your toy examples can't be run against a real server, I have zero expectation of it working well for non-trivial examples.
I mean, this is actually even better. You can expand the mock-server down below and see what's happening.
Reducing server load makes sense. Use a CDN or proxy, and aggressively cache responses.
Adding an entirely faked backend response, so that the examples don't actually show the real traffic it would generate is both adding complexity to the demo, and potentially giving a misleading picture of how usable the library is: for all we know, it's too slow to use as the examples show, because a real HTTP request to a backend is going to be slower than a javascript function returning a pre-determined string.
The library is displayed fully functionable, but other parts are mocked. Your example is asinine.
Developer tools in browsers are a thing that exist, and show you what is actually requested.