Home

Awesome

<div> <a href="https://www.npmjs.com/package/hmpl-js"> <img width="200" src="https://raw.githubusercontent.com/hmpl-language/media/refs/heads/main/logo_readme.png" alt="hmpl"> </a> </div> <div>

npm-version minzipped size codecov issues

x.com product hunt discord

Server-oriented customizable templating for JavaScript

</div> <div><a href="https://hmpl-lang.dev">Website</a> • <a href="https://hmpl-lang.dev/introduction.html">Documentation</a> • <a href="https://codesandbox.io/p/sandbox/basic-hmpl-example-dxlgfg">Demo Sandbox</a> • <a href="https://hmpl-lang.dev/examples.html">Examples</a></div><br/> <div>hmpl is a small template language for displaying UI from server to client. It is based on <em>customizable</em> requests sent to the server via <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">fetch</a> and processed into ready-made HTML. The language is syntactically object-based and integrated with <a href="https://www.npmjs.com/package/json5">JSON5</a>. Reduce the size of your javascript files and display the same UI as if it was written in a modern framework.</div>

Usage

import hmpl from "hmpl-js";

const templateFn = hmpl.compile(
  `<div>
      <button data-action="increment" id="btn">Click!</button>
      <div>Clicks: {{ src: "/api/clicks", after: "click:#btn" }}</div>
  </div>`
);

const clicker = templateFn(({ request: { event } }) => ({
  body: event.target.getAttribute("data-action"),
})).response;

document.querySelector("#app").append(clicker);

Why HMPL?

Using HMPL, you can multiply reduce the size of the application bundle. Full customization of the request based on the modern fetch standard, as well as support for all the functionality necessary for modern work in applications (request indicator, sending by event, automatic generation of body for the form, caching) and the syntax of the object in the markup, which requires a minimum number of characters, will help to build interaction with the server and client as efficiently as possible.

Features

Installation

hmpl can be installed in several ways, which are described in this section. This tool is a simple javascript file that is connected in the usual way through a script, or using the import construct in an environment that supports this (webpack build, parcel build etc.). Also, starting from version 2.2.0, the connection of the JSON5 module is required. The first and easiest way is to install using a CDN.

Package Manager

This method involves downloading through npm or other package managers.

npm i hmpl-js

Node.js is required for npm.

Along the path node-modules/hmpl/dist you can find two files that contain a regular js file and a minified one.

CDN

This method involves connecting the file through a third-party resource, which provides the ability to obtain a javascript file from npm via a link.

<script src="https://unpkg.com/json5/dist/index.js"></script>
<script src="https://unpkg.com/hmpl-js/dist/hmpl.min.js"></script>
<!--
  integrity="sha384-..."
  crossorigin="anonymous"
-->

This resource could be unpkg, skypack or other resources. The examples include unpkg simply because it is one of the most popular and its url by characters is not so long.

Manual download

You can install the package by simply downloading it as a file and moving it to the project folder.

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

If, for some reason, you do not need the minified file, then you can download the full file from this link.

<script src="https://unpkg.com/json5/dist/index.js"></script>
<script src="./hmpl.js"></script>

The non-minified file is larger in size, but it is there as it is with all the formatting.

Ecosystem

<a href="https://www.npmjs.com/package/hmpl-loader"><img src="https://raw.githubusercontent.com/hmpl-language/media/refs/heads/main/Webpack.svg" alt="hmpl-loader" height="40"/></a> <a href="https://marketplace.visualstudio.com/items?itemName=hmpljs.hmpl"><img src="https://raw.githubusercontent.com/hmpl-language/media/refs/heads/main/VS%20Code.svg" height="40" alt="vs-code extension"/></a>

Community support

The documentation contains main information on how the HMPL template language works. If you have any questions about how HMPL works, you can use the following resources:

You can also ask your question on Stack Overflow and address it in the resources described above.

Contribution

We have a Contributing Guide that describes the main steps for contributing to the project.

Thank you to all the people who have already contributed to HMPL, or related projects!

Fossa status

FOSSA Status