No video

HTMX JSON API Example with Wordpress

  Рет қаралды 769

Stephen Blum

Stephen Blum

Күн бұрын

The issue with HTMX arises from its need to interact with a server that returns pre-rendered html, especially when you're handling APIs and making API calls with external servers. Usually, you want to keep exchanged data in bytes rather than as document data, and html is a document data mark-up. You can make this work with HTMX, though it is against common practice.
Most APIs include data serialization to keep data as bytes, typically using json as the returned data. To work with HTMX, you'd normally need to update or create a wrapper for the API to return html instead of json, which requires extra server work. Alternatively, you can have data view patterns where you include parameters at the end of the API call URL specifying the data return format.
This could be json, seabor, or even html. Whilst this is possible, it's a lot of extra server work. Wouldn't life be easier without having to do that?
You could interact with any API and still use HTMX. Luckily, you can. HTMX simplifies this process by including a client-side template extension, complete with a range of supported templates. These templates use languages such as mustache and handlebars, which are very common and probably the most useful in this scenario.
So, if you want to interact directly with json APIs, check out the client-side template extension in HTMX. This extension allows you to use json as your input data and incorporate that into your HTMX application. Most server APIs are written in or return json data. HTMX prefers you to use HTML returned directly from the server, which means additional server work. with the client-side templates extension, you can transform json and xml requests into html using a client-side template.
This extension is fantastic and makes interaction with HTMX much simpler. You can use this process instead of having a server return HTML data. It's an easy installation requiring you to simply paste in a script alongside your other scripts.
If you're working with platforms like WordPress with a robust API, you can interact with all the components stored in the WordPress database. All your data is referenceable by ID and paginated, allowing you to fetch, update, or even import data. You can also achieve this with the HTMX framework, which makes it super easy to build front-end web apps without the need to write any JavaScript at all. it does want you to import all of your API calls using HTML, which you can do with a client-side templates extension that supports transforming json or xml into html directly.
Now you can consume any API, including the WordPress API, obliging HTMX. With this process, you're able to directly interact with JSON APIs using the HTMX framework. This gives you more flexibility on the front end as you can capture data from a JSON API and place it anywhere you want more easily rather than simply getting a bundle of HTML from the server. The front-end flexibility comes with interacting with any data part and placing that data wherever you want on the UI. You can download the data from the WordPress API for testing purposes, put it into the same directory calling it WordPressData.json.
Then, using a template engine like mustache, iterate through lists of items. In WordPress, iterate the number of posts; in mustache, all you have to do is type posts and then any data inside the list would be an object. Then, you can just import your template engine to be used with mustache, making the whole looping process easy.
Once this process is done, all you have to do is open your HTML page in a web browser, run a server, and you have a list of the WordPress posts on the page using HTMX. Please note, you need to include dependencies like the HTMX core library, the client-side template extension, and the template engine you're using, which is mustache, in this instance. Ensure that each extension is being used here, set your button to fetch the data. In an instance where JSON data is stored locally, the swap can be inner HTML, but the target is going to be the ID of content.
You're going to use the mustache template, iterate over all the posts in the return of JSON, and print out just the title, the author, and the ID of the post. That's all. The HTML needs a minor modification so it can interpret this new syntax, which provides more frontend flexibility.
You are able to capture the data from a JSON API and put that wherever you want more easily than directly getting the bundle of HTML from the server. This straight-forward process is all you need to interact directly with JSON APIs using HTMX framework.

Пікірлер: 20
@tuulikk9193
@tuulikk9193 4 ай бұрын
This is inspirational and I like the enegy you bring to this video, just like any other video I have seen from you.
@StephenBlum
@StephenBlum 4 ай бұрын
Inspirational, that is great feedback thank you! 🎉😄
@PouriyaJamshidi
@PouriyaJamshidi 5 ай бұрын
high quality content mate. Keep up the good work
@StephenBlum
@StephenBlum 5 ай бұрын
thank you! 😄
@huntrolly2190
@huntrolly2190 14 күн бұрын
Thanks❤
@StephenBlum
@StephenBlum 14 күн бұрын
absolutely! ❤ HTMX is amazing, truly. The simplicity and performance covers 99% of all necessary capabilities that you'd need from React. And it's much faster and easier to use for more people. It's a great addition to the developer ecosystem 🎉🙌
@nykowow
@nykowow 4 ай бұрын
Very interesting, thanks!
@StephenBlum
@StephenBlum 4 ай бұрын
Happy to share! 😄 This is my favorite way to use HTMX with a template engine that consumes JSON APIs.
@nykowow
@nykowow 4 ай бұрын
@@StephenBlum would be interesting to see a routing solution suitable for WordPress, I am trying the plugin "HTMX API for WP" in conjunction with the theme "HTMX Theme for WordPress" and they seem to work well but only in conjunction, I'd rather develop a custom theme from scratch, without plugins and using htmx
@AdaptorLive
@AdaptorLive 2 ай бұрын
I understand why you would want to do this but not sure you should. Doesn't this go against the whole hypermedia paradigm which is what makes HTMX unique?
@StephenBlum
@StephenBlum 2 ай бұрын
Yes you are correct. This approach departs from the simplicity of HTMX. Ideally with HTMX we use just HTML tags with attributes as configuration. The problem comes with interacting with non-HTMX compatible APIs. HTMX requires APIs to respond with HTML tags. Most APIs use JSON. In order to consume JSON, we have to use the native module included with HTMX. This is `client-side-templates` module. It allows you to fetch data from JSON APIs, then consume the response by converting JSON to HTML. It requires learning a DSL template language. Which is beyond the original simplicity intent of HTMX.
@AdaptorLive
@AdaptorLive 2 ай бұрын
​​@@StephenBlumThanks for your response. I found your video because I work with WordPress and recently found out about HTMX. Really hyped about trying it out. It seems like such an easy way to make my WordPress sites more dynamic, it almost feels like cheating. I guess my comment was more philosophical because it feels kinda icky when the first thing you do when integrating new tech is to just throw away what makes it unique but to be honest, what you showed in this video is just too good to not try. The alternative would be to make all the ajax endpoints myself which kind of feels weird when there's already this whole API in place, ready to go. The fact that the template stuff is on the HTMX site itself is also reassuring. I guess I will just have to try it out. Before I do, I'd like to know why XSLT is so bad?
@StephenBlum
@StephenBlum 2 ай бұрын
​@@AdaptorLive that's great to hear! you got it, that's exactly the set of ideas around this video. Taking advantage of the simplicity of HTMX as best we can in combination with the WordPress JSON APIs. It's a great approach that allows you to remain 100% focused on front-end development 🎉😄 XSLT is actually a foundation today in all browser. you can create any custom and style it with CSS. This also works with HTMX natively. Pretty nifty! The great part about XSLT is that it keeps you within HTML syntax. Very powerful and for sure it's keeping you closer to the wholistic HTML-first development approach. 🙌
@ballpen9157
@ballpen9157 5 ай бұрын
Thank you so much!!
@StephenBlum
@StephenBlum 5 ай бұрын
🎉😄
@davidbrabyn1
@davidbrabyn1 4 ай бұрын
What's the advantage of this (which has its own costs -- 3 files) over using WP's own php functions? Avoiding a database query?
@StephenBlum
@StephenBlum 4 ай бұрын
Hi David, you have a good question! 😄 When you are working with WordPress, it already has it's own template engine. You don't need to worry about HTMX. The video is less about WordPress, and more about HTMX. HTMX has the ability to work with JSON APIs which is a powerful feature. The WordPress API was used solely as an example scenario. There may be a situation where you may want to build an external system to your WP website, and a UI can be built to manage the WP database via the WP API using HTMX and the JSON API and Template Plugin. You do have a good point that there may not be a notable benefit in relation to WordPress via HTMX.
@davidbrabyn1
@davidbrabyn1 4 ай бұрын
@@StephenBlum Got it thx. I have to admit, as much as it seems magical, I am struggling to find uses cases for htmx in WP. I am sure there are some and I am just not very imaginative. Great video though.
@StephenBlum
@StephenBlum 4 ай бұрын
@@davidbrabyn1 😊 nice! thank you!
@blazingelse9104
@blazingelse9104 2 ай бұрын
@@davidbrabyn1 As far as I understand it, HTMX does not compete with server side rendered Websites at all. It is a competitor to all the Reacts, Angulars und Vues. If your rendering does not happen on client side, there really is no use case.
HTML Templates Instead Of Reactivity | Prime Reacts
12:42
ThePrimeTime
Рет қаралды 111 М.
Мы сделали гигантские сухарики!  #большаяеда
00:44
Get 10 Mega Boxes OR 60 Starr Drops!!
01:39
Brawl Stars
Рет қаралды 17 МЛН
👨‍🔧📐
00:43
Kan Andrey
Рет қаралды 10 МЛН
女孩妒忌小丑女? #小丑#shorts
00:34
好人小丑
Рет қаралды 62 МЛН
UI Libraries Are Dying, Here's Why
13:28
Theo - t3․gg
Рет қаралды 302 М.
Automate AI Research with Crew.ai and Mozilla Llamafile
11:43
Practical AI through Prototypes
Рет қаралды 11 М.
React vs HTMX: Why we chose HTMX?
5:01
Kodaps Academy
Рет қаралды 9 М.
r/webdev BANS HTMX??
5:28
ThePrimeTime
Рет қаралды 148 М.
Introducing The GoTTH Stack - Go, Tailwind CSS, Templ & HTMX
28:29
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 353 М.
"The Life & Death of htmx" by Alexander Petros at Big Sky Dev Con 2024
23:01
Montana Programmers
Рет қаралды 52 М.
Мы сделали гигантские сухарики!  #большаяеда
00:44