Тёмный

HTMX JSON API Example with Wordpress 

Stephen Blum
Подписаться 2,3 тыс.
Просмотров 903
50% 1

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.

Опубликовано:

 

3 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 22   
@tuulikk2
@tuulikk2 6 месяцев назад
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 6 месяцев назад
Inspirational, that is great feedback thank you! 🎉😄
@PouriyaJamshidi
@PouriyaJamshidi 6 месяцев назад
high quality content mate. Keep up the good work
@StephenBlum
@StephenBlum 6 месяцев назад
thank you! 😄
@hakim_jazuli
@hakim_jazuli Месяц назад
this is good for already made json api end points... for new project, sticking with html api is arguably better (at least on performance)... and even that you already made json api end points... Carson Gross, "the" dude behind the HTMX, still recomends to made html api end point parallel with your api... the argument being, json api should be stable and versionable... while html api is to be fluid, because it handles the ui too... therefore using client side templating for htmx, is only recomended, if you are planning to make non-web native app, so you don't have to write the api multiple times... I mean, if the htmx it self supports this kind of client side extention is slightly not making sense... the core theory is about HATEOAS, as in Hypermedia As The Engine Of Application States... for better or worse... using html as sole handler for the application states gave you advantage that no pure-client side framework, literally NONE of them, and will never, has... which is that, the unusable string will never be sent to the client, only backend approved html string is being sent... example, delete post button, will never be sent to the client, if the user have no authority for that... in client side framework however, if you look at the template tag or the bundled js file, you will definitely can check that for sure... the good thing is, htmx developer doesn't add this templating module to the core functionality, but rather as extentions...
@StephenBlum
@StephenBlum Месяц назад
Nice! Great insight. This makes a lot of sense 😄 Using HTML API to populate the UI has a lot more benefits. You make a great point. Only the needed UI elements are transmitted to the user. If the user doesn't have permission or never needs to use a UI component, then that user won't ever load unused UI. Very nice! Thank you for this detail. HTMX is showing better and better every day 🙌🎉
@AdaptorLive
@AdaptorLive 3 месяца назад
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 3 месяца назад
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 3 месяца назад
​​@@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 3 месяца назад
​@@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. 🙌
@huntrolly2190
@huntrolly2190 Месяц назад
Thanks❤
@StephenBlum
@StephenBlum Месяц назад
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 5 месяцев назад
Very interesting, thanks!
@StephenBlum
@StephenBlum 5 месяцев назад
Happy to share! 😄 This is my favorite way to use HTMX with a template engine that consumes JSON APIs.
@nykowow
@nykowow 5 месяцев назад
@@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
@ballpen9157
@ballpen9157 6 месяцев назад
Thank you so much!!
@StephenBlum
@StephenBlum 6 месяцев назад
🎉😄
@davidbrabyn1
@davidbrabyn1 5 месяцев назад
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 5 месяцев назад
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 5 месяцев назад
@@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 5 месяцев назад
@@davidbrabyn1 😊 nice! thank you!
@blazingelse9104
@blazingelse9104 3 месяца назад
@@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.
Далее
You don't need a frontend framework
15:45
Просмотров 127 тыс.
🎙Пою РЕТРО Песни💃
3:05:57
Просмотров 1,3 млн
r/webdev BANS HTMX??
5:28
Просмотров 151 тыс.
The Simplest Tech Stack
9:38
Просмотров 120 тыс.
UI Libraries Are Dying, Here's Why
13:28
Просмотров 313 тыс.
What is Serverless Backend
13:16
Просмотров 516