DEV Community

Cover image for I replaced the HTMX in the project to this module🔥
Anthony Max
Anthony Max Subscriber

Posted on

I replaced the HTMX in the project to this module🔥

TL;DR

I already wrote an article on how this module can be used to replace Next.js, but it was more of a light version in some cases.

Now, finally, we get to the specifics, where the replacement is completely relevant and not far-fetched. Both modules work with HTML from the server and have a similar concept, so this is definitely a good fit.

Today, we will be replacing HTMX in the project with HMPL.

Well, let's get started! 🏎️


⚙️ From simple examples

Let's start with the simplest example we can imagine - a clicker. You could do something like this:

<body>
  <div class="clicker-container">
    <h1>HTMX Clicker</h1>
    <div class="click-count" id="counter" hx-swap-oob="true">0</div>
    <button
      class="click-button"
      hx-post="/click"
      hx-target="#counter"
      hx-swap="innerHTML"
    >
      Click!
    </button>
  </div>
</body>
Enter fullscreen mode Exit fullscreen mode

Looks good at first glance, and most importantly, it is functional. But now, let's take an example on HMPL.js:

<body></body>
<script>
  const templateFn = hmpl.compile(`
  <div class="clicker-container">
    <h1>HMPL Clicker</h1>
    <div class="click-count" id="counter">
      {{#request src="/click" after="click:#btn"}}
      {{/request}}
    </div>
    <button id="btn">Click!</button>
  </div>
`);
  const clicker = templateFn().response;
  document.querySelector("body").append(clicker);
</script>
Enter fullscreen mode Exit fullscreen mode

As we can see, we have two different approaches, where one module is completely dependent on the current DOM, and the second one is not, but is generated via JS. But, we see that in general, it is the same interface with a common approach. Only here, the first and main problem is that in the first case we have the minimum possible set of settings for the request.


🔍 Problems (intentional)

In HTMX, it was specially made so that the developer would depend minimally on js, so this is where the most serious problem of this module comes from - practically zero customization of the request.

So, yes, of course, you can send the body like this:

<button
  class="click-button"
  hx-post="/click"
  hx-target="#counter"
  hx-swap="innerHTML"
  hx-vars='{"action": "increment"}'
>
  Click!
</button>
Enter fullscreen mode Exit fullscreen mode

But still, if we want something more, we see that it is impossible or difficult to implement. And, once again, this is intentional, because the whole point of the module is ease of use.

Also, the second problem is that we have requests, but they are sent via XMLHTTPRequest, a standard that is over 25 years old. Yes, the last addition was in the early 10s, but the fact is.

In HMPL, the almost completely customizable fetch is used, that is, consider that everything new in ECMAScript is already supported by default.


🔗 Complex example

Let's use a more complex example to show how else you can replace HTMX in a project.

<div id="wrapper">
  <form 
    id="form"
    hx-post="/api/register"
    hx-target="#response"
    hx-indicator="#loading-indicator"
    hx-swap="innerHTML"
    hx-include="this"
  >
    <div class="form-example">
      <label for="login">Login: </label>
      <input type="text" name="login" id="login" required /><br/>
      <label for="password">Password: </label>
      <input type="password" name="password" id="password" required />
    </div>
    <div class="form-example">
      <input type="submit" value="Register!" />
    </div>
  </form>

  <div id="response"></div>

  <div id="loading-indicator" class="htmx-indicator">
    <p>Loading...</p>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Here we now have a form that we send to the server. The hx-include attribute includes the data as FormData. Also, we have a request indication. Now, let's try to do the same in HMPL:

<body></body>
<script>
  const templateFn = compile(
    `<div id="wrapper">
  <form onsubmit="function prevent(e){e.preventDefault();};return prevent(event);" id="form">
    <div class="form-example">
      <label for="login">Login: </label>
      <input type="text" name="login" id="login" required /><br/>
      <label for="password">Password: </label>
      <input type="password" name="password" id="password" required />
    </div>
    <div class="form-example">
      <input type="submit" value="Register!" />
    </div>
  </form>
  <p>
    {{#request
      src="/api/register"
      after="submit:#form"
      repeat=true
    }}
     {{#indicator trigger="pending"}}
        <div class="indicator">
          <p>Loading...</p>
        </div>
     {{/indicator}}
    {{/request}}
  </p>
</div>`
  );
  const initFn = (ctx) => {
    const event = ctx.request.event;

    return {
      body: new FormData(event.target, event.submitter),
      credentials: "same-origin",
    };
  };
  const obj = templateFn(initFn);
  document.querySelector("body").append(obj.response);
</script>
Enter fullscreen mode Exit fullscreen mode

Here it is clearly visible that we can do the same as in HTMX, but there is a small but, that we can work with the form, take any data from there and modify our request.


📊 Size comparison

I won't keep silent about this point, but it's still worth evaluating from this side. With HTMX you will write less. Let's compare the size of applications with a simple example:

📦 HTMX:

size 1

📦 HMPL:

size 2

Because everything is so minimal, HTMX is the undisputed leader in terms of size, but you can see that in this example the size of the application is not very critically different.

Although, compared to others, HMPL generally shows good results in terms of dimensionality:

comparison

But, of course, if we reduce the capabilities of js, essentially doing the bare minimum, then the size of the source files will be smaller.


✅ Conclusion

Of course, you can continue to use HTMX, but in this article I wanted to give an alternative to the existing option. Tasks are different and people do not always need the bare minimum in the size of the application and they are not always ready to sacrifice functionality because of this. When working with the server, you will still have to customize the request one way or another, if we are talking about large applications. But, everything depends on the task.


Thank you very much for reading this article ❤️!

What do you think about this alternative? It will be interesting to know in the comments!

Useful link:

Top comments (13)

Collapse
 
dotallio profile image
Dotallio

Love this deep dive into how much more flexibility HMPL offers on request customization compared to HTMX.

Curious if there are still any scenarios where you'd reach for HTMX over HMPL, or have you fully switched?

Collapse
 
anthonymax profile image
Anthony Max

When size is the only thing that matters, otherwise I choose HMPL

Collapse
 
nevodavid profile image
Nevo David

pretty cool seeing your breakdown, makes me rethink if i’m sticking with my stack out of laziness or actual need - you ever feel like it’s just habit that keeps you on one tool for ages?

Collapse
 
anthonymax profile image
Anthony Max

Yes

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
anthonymax profile image
Anthony Max

Well, I just wrote an article about this.

Collapse
 
parag_nandy_roy profile image
Parag Nandy Roy

Loved the alternative..

Collapse
 
hhvdblom profile image
hhvdblom

Like the styling from HTMX. Like the KISS convention from HTMX. Like the HTML approach from HTMX. So Hmpl is not for me.

Collapse
 
anthonymax profile image
Anthony Max

Well, here it depends on what someone needs in the task - yes. But, it does not cancel the fact that with HTMX you will not be able to customize the request normally.

Collapse
 
anthonymax profile image
Anthony Max

I think this is a great replacement.

Collapse
 
axrisi profile image
Nikoloz Turazashvili (@axrisi)
  • Overview: The discussion emphasizes HMPL as a viable replacement for HTMX in web development, providing similar functionalities while being more customizable.

  • Modules Comparison

    • Both HTMX and HMPL work with HTML from the server
    • Similar concept but different implementations
    • HTMX is minimalistic, while HMPL offers more customization
  • Simple Clicker Example

    • HTMX Implementation: A basic counter with limited settings
      • Uses XMLHTTPRequest for request handling
      • Limited customization options
    • HMPL Implementation: Compiles a similar clicker using JavaScript
      • Allows greater flexibility in request handling
  • Key Problems Identified

    • HTMX
      • Minimal JavaScript dependency makes customization difficult
      • Requests are sent using a 25-year-old XMLHTTPRequest standard
    • HMPL
      • Utilizes customizable fetch for modern JavaScript features
  • Complex Form Example

    • HTMX: Form submissions with basic functionality
      • Uses hx-include to include form data
    • HMPL: Replicates form behavior with enhanced request customization
      • Capable of modifying requests by extracting form data.
  • Size Comparison

    • HTMX leads in application size efficiency
      • Smaller application sizes when using minimal settings
    • HMPL shows decent dimensionality compared to alternatives.
  • Conclusion

    • Users can still opt for HTMX, but HMPL offers an alternative for those needing more functionality.
    • The choice depends on the complexity of the application and the specific needs of the developer.

made with love by axrisi
axrisi.com

Some comments may only be visible to logged-in visitors. Sign in to view all comments.