Lädt...

🔧 HMPL - new template language for fetching HTML from API


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In this article I will talk about a new template language called HMPL. It allows you to easily load HTML from the API, eliminating a ton of unnecessary code.

The main goal of hmpl.js is to simplify working with the server by integrating small request structures into HTML. This can be compared to how, in files with a php extension, you could work with the response from the server received through a php request, but at the same time work with it directly through javascript. Using the example of simply getting the title from a button, you can understand how this template language can simplify your work

This template language allows you to repeat the string template that was specified. That is, in code it looks like this:

import { compile } from "hmpl-js";

const templateFn = compile(
  `<div>
    <request src="/api/test"></request>
  </div>`
);

const wrapper = document.getElementById("wrapper");

const obj1 = templateFn();

const obj2 = templateFn();

wrapper.appendChild(obj1.response);
wrapper.appendChild(obj2.response);

The module is based on fetch api, which allows you to work with the server using modern JS tools.

To interact with the fetch API, a settings object was also created, which is based on the RequestInit type. Example code:

const elementObj = templateFn({
  method: "POST",
  mode: "cors",
  cache: "no-cache",
  credentials: "same-origin",
  headers: {
    "Content-Type": "text/html",
  },
  redirect: "follow",
  get: (prop, value) => {},
  referrerPolicy: "no-referrer",
  body: JSON.stringify(data),
  signal: new AbortController().signal,
  integrity: "...",
  window: null,
  refferer: "about:client",
});

The syntax of the template language itself makes it possible to use files with the .hmpl extension to create practical and understandable project file structures, as well as to separate regular HTML from “modular” ones.

mac-os file structure

The module is very small in size (version 1.0.9). It weighs less than 100 kilobytes in npm. The minified file itself weighs even less.

size

The module has several connection options for maximum ease of use in tasks:

<script src="https://unpkg.com/hmpl-js/dist/hmpl.min.js"></script>

or

{
   "dependencies": {
    "hmpl-js": "latest",
   }
}

or

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.hmpl$/i,
        use: ["hmpl-loader"],
      }
    ]
  }
}

Examples of simple projects on the module:

https://github.com/hmpljs/examples

Other useful links:

https://hmpljs.github.io
https://github.com/hmpljs/hmpl-loader
https://github.com/hmpljs/hmpl
https://www.youtube.com/@antonmak1

...

🔧 ❄️Introducing the HMPL Template Language


📈 41.18 Punkte
🔧 Programmierung

🔧 🎙️We build HMPL to help developers make web apps smaller in size🔥


📈 24.4 Punkte
🔧 Programmierung

🔧 We build HMPL to help developers make web apps smaller in size🔥


📈 24.4 Punkte
🔧 Programmierung

🔧 🔥We build HMPL to help developers make web apps smaller 🗄️ in size


📈 24.4 Punkte
🔧 Programmierung

🔧 🌷Creating a Gallery App in JavaScript with HMPL


📈 24.4 Punkte
🔧 Programmierung

🔧 🌷Creating a Gallery App in JavaScript with HMPL


📈 24.4 Punkte
🔧 Programmierung

🔧 🔥Introducing the HMPL VS Code Extension


📈 24.4 Punkte
🔧 Programmierung

🔧 🔥Introducing the HMPL VS Code Extension


📈 24.4 Punkte
🔧 Programmierung

🔧 🔥Introducing the HMPL VS Code Extension


📈 24.4 Punkte
🔧 Programmierung

🔧 HMPL integration with JSON5


📈 24.4 Punkte
🔧 Programmierung

🔧 🐜 Since CSS has changed the logo, it's time for HMPL to change the logo too!


📈 24.4 Punkte
🔧 Programmierung

🔧 🔥HMPL — best alternative to HTMX


📈 24.4 Punkte
🔧 Programmierung

🔧 HMPL - best alternative to HTMX


📈 24.4 Punkte
🔧 Programmierung

🔧 Differences between HMPL and HTMX


📈 24.4 Punkte
🔧 Programmierung

🔧 How to reduce javascript file size on client using HMPL?


📈 24.4 Punkte
🔧 Programmierung

🔧 How to work with .hmpl file extension in javascript?


📈 24.4 Punkte
🔧 Programmierung

🔧 HMPL.js on DevHunt!🔥


📈 24.4 Punkte
🔧 Programmierung

🔧 Fetching Blog Posts in Next.js Using the google blogger.to API


📈 21.94 Punkte
🔧 Programmierung

🔧 Using APIs in JavaScript: Fetching Data from a Public API 🌐📊


📈 21.94 Punkte
🔧 Programmierung

🔧 "Day 18: Deep Dive into React Hooks, API Fetching, and DSA!"


📈 21.94 Punkte
🔧 Programmierung

🔧 Fetching Blog Posts in Next.js Using the DEV.to API


📈 21.94 Punkte
🔧 Programmierung

🔧 Build YouTube API Data Fetching Using Rust


📈 21.94 Punkte
🔧 Programmierung

🔧 How to Fix Memory Leakage in Vue.js API Fetching?


📈 21.94 Punkte
🔧 Programmierung

🔧 Implementing Robust Retry Logic in Laravel Jobs for Reliable API Data Fetching


📈 21.94 Punkte
🔧 Programmierung

🔧 How to solve CORS error while fetching an external API? (Solution Explained)


📈 21.94 Punkte
🔧 Programmierung

🔧 Mastering API Requests in JavaScript: A 2024 Guide to Fetching Data


📈 21.94 Punkte
🔧 Programmierung

🔧 Building a Sequential Data Fetching API with JavaScript


📈 21.94 Punkte
🔧 Programmierung

🔧 Fetching Calendly API Events Using Express and Node.js


📈 21.94 Punkte
🔧 Programmierung

🔧 Tutorial 15: Fetching and Displaying JSON Data from a REST API in Swift


📈 21.94 Punkte
🔧 Programmierung

🔧 Get API &amp; Fetching Data from MongoDB in Go - 05


📈 21.94 Punkte
🔧 Programmierung

🔧 Creating Dynamic Pages with Next.js and Fetching Data from a REST API


📈 21.94 Punkte
🔧 Programmierung