MIT License

项目介绍

hmpl-language/hmpl 是一个托管在 GitHub 上的开源项目,主要使用 TypeScript 编写,采用 MIT License 许可证,目前获得 960 个 Star、120 次 Fork。

仓库全名
hmpl-language/hmpl
开源许可证
MIT License
Star 数
960
Fork 数
120
未关闭 Issue
4

README 摘要

Server-oriented customizable templating for JavaScript 🌐 Website • 📄 Docs • 🖥️ Demo Sandbox • ⚙️ Examples Introduction HMPL.js provides the flexibility to build server-driven templates with minimal JavaScript. With its block-based syntax, customizable fetch requests, and built-in support for forms, events, and time-based syncing you can deliver dynamic user interfaces without relying on a heavy framework. HMPL…

完整内容见 GitHub 原始 README 或本页 。

常见问题

hmpl 用什么语言开发?

主要使用 TypeScript 编写,采用 MIT License 许可证,目前有 960 个 Star。

hmpl 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 hmpl 的源码?

可以直接克隆仓库:git clone https://github.com/hmpl-language/hmpl.git;安装与运行步骤见仓库 README。

hmpl 最近还在维护吗?

仓库最近一次更新为 2月27日,当前有 4 个未关闭 Issue;Star 数为 960,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

🐜 HMPL.js is a lightweight server-oriented template language for JavaScript. Fetch HTML, render it safely, and keep apps dynamic, modern, and small.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

Astro
CSS
HTML
JavaScript
MDX
Ruby
SCSS
TypeScript
Vue

Release

README

hmpl

Server-oriented customizable templating for JavaScript

npm-version codecov stars downloads discord x.com


HMPL - Lightweight server-oriented template language for JavaScript | Product Hunt

Introduction

HMPL.js provides the flexibility to build server-driven templates with minimal JavaScript. With its block-based syntax, customizable fetch requests, and built-in support for forms, events, and time-based syncing you can deliver dynamic user interfaces without relying on a heavy framework. HMPL integrates with JSON5 for expressive object syntax and DOMPurify for safe HTML rendering, all in just a few kilobytes.

Example

<div>
  {{#request src="/api/my-component.html"}} {{#indicator trigger="pending"}}
  <p>Loading...</p>
  {{/indicator}} {{/request}}
</div>

Try HMPL online at: hmpl-playground

Basic usage

import hmpl from "hmpl-js";

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

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

document.querySelector("#app").append(clicker);
Explain this!
import hmpl from "hmpl-js"; // Import the HMPL library

// Compile an HMPL template with dynamic behavior
const templateFn = hmpl.compile(
  `<div>
      <button data-action="increment" id="btn">Click!</button>
      <!-- This div will update with the click count from /api/clicks -->
      <div>Clicks: {{#request src="/api/clicks" after="click:#btn"}}{{/request}}</div>
      <!-- Also, you can write in short: {{#r src="..."}}{{/r}} -->
  </div>`
);

// Generate a response handler for the template
// In the original object, we will have the following: { response: div, status: 200 }
const clicker = templateFn(({ request: { event } }) => ({
  // Send a JSON payload with the action from the button's data attribute
  body: JSON.stringify({ action: event.target.getAttribute("data-action") })
})).response;

// Append the dynamically generated element to the #app container
document.querySelector("#app").append(clicker);

In this example, we create a dynamic clicker component in which, when a button is pressed, we will receive the value of the current clicks that will come from the server. The advantage of this approach is that we can take out not only data in the form of Text, but also entire components and even pages!

Usage with DOM

If you need an option without using js, then by connecting the additional hmpl-dom module you can do this.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
  </head>
  <body>
    <main>
      <template hmpl>
        <div>
          {{#request src="/api/my-component.html"}} {{#indicator
          trigger="pending"}}
          <p>Loading...</p>
          {{/indicator}} {{/request}}
        </div>
      </template>
    </main>
    <script src="https://unpkg.com/json5/dist/index.min.js"></script>
    <script src="https://unpkg.com/dompurify/dist/purify.min.js"></script>
    <script src="https://unpkg.com/hmpl-js/dist/hmpl.min.js"></script>
    <script src="https://unpkg.com/hmpl-dom/dist/hmpl-dom.min.js"></script>
  </body>
</html>

This way, components from the server are mounted into the DOM without having to add them manually.

Why HMPL?

Using template language capabilities, 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. App size comparison (bytes):

Also, HMPL can be a great alternative to popular tools such as HTMX and Alpine.js.

Features

  • Customizable: Send a custom request to the server when receiving the UI
  • Memory Preserving: Reduce file sizes on the client by several times
  • Based on Fetch API: Use a modern standard instead of XMLHTTPRequest
  • Server-oriented: Work with the server directly through markup and with a little js
  • Generate thousands of DOM nodes from a single template: Work with large components not only on the server but also on the client
  • Simple: Get ready-made UI from the server by writing a couple of lines of familiar object syntax
  • Protected from XSS attacks: Enable incoming server HTML sanitization with DOMPurify and work with it safely
  • Flexible: Can be used in almost any project due to not only working through a script, but also working in files with the .hmpl extension
  • Integrated with JSON5: Flexible writing of objects by docs as in vanilla js, as well as the reliability of the parser used by millions of people
  • Small bundle size: Lots of functionality in a couple of kilobytes

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.).

Note 注意

Starting with version 2.2.0, the JSON5 module needs to be connected, and starting with version 2.2.5, the DOMPurify module also needs to be connected. 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

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.min.js"></script>
<script src="https://unpkg.com/dompurify/dist/purify.min.js"></script>
<script src="https://unpkg.com/hmpl-js/dist/hmpl.min.js"></script>
<!--   
  integrity="..."
  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.

Through the starter template project

There is a starter project on Vite.

npx degit hmpl-language/hello-hmpl-starter hello-hmpl

Based on it, you can make web applications.

Official Tools

vs-code-extension VS Code Extension
Syntax highlighting and tools for HMPL
vite-plugin-hmpl Vite Plugin
Seamless .hmpl integration with Vite
hmpl-loader Webpack Loader
Compile .hmpl files inside Webpack

But we will be glad if you make your own :)

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:

  • GitHub - In the discussion and issues sections you can ask any question you are interested in
  • Discord - You can ask your question in the thematic channel “support”
  • 𝕏 (Twitter) - There is a lot of interesting stuff there, concerning the template language and not only :)

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!

Star History

Star History Chart

Roadmap

The project has a roadmap where you can see the plans for future development.

License

Released under the MIT


💎 Star this repo • 💻 Try HMPL.js • 💬 Join Discord

This project has been developed with contributions from many amazing community developers!

评论

暂无评论