htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Tutorial for using HX-Request header

Open benpate opened this issue 4 years ago • 1 comments

As mentioned on Discord, it would be helpful to have a tutorial that shows how to use the HX-Request header to modify returns from the server, showing a full page if this value is not present, and only returning a partial page if it is.

benpate avatar Feb 12 '21 17:02 benpate

I’m not sure about tutorial, but the book mentions something similar to this:

Htmx takes advantage of this feature of HTTP and adds additional headers and, therefore, additional context to the HTTP requests that it makes. This allows you to inspect those headers and choose what logic to execute on the server, and what sort of HTML response you want to send to the client.

Here is a table of the HTTP headers that htmx includes in HTTP requests: … Let’s add some conditional logic to our controller to look for that header and, if the value is search, we render only the rows rather than the whole index.html template:

https://hypermedia.systems/more-htmx-patterns/

aburd avatar Feb 23 '24 13:02 aburd