Skip to content
TwitterDiscordGitHub

hmpl

The global hmpl object serves as the primary interface for template compilation and request serialization. This object is automatically available in the global scope without requiring explicit import (if connected via script).

MethodsTypeDescription
compileHMPLCompileTransforms template strings into executable template functions
stringify(info: HMPLRequestInfo) => stringSerializes request configuration objects to component string
import hmpl from "hmpl-js";
const { compile, stringify } = hmpl;

The compile method processes HMPL template strings into executable template function.

ArgumentTypeDescriptionRequired
templatestringHMPL template string containing HTML and requestsYes
optionsHMPLCompileOptionsCompilation configuration parametersNo
const templateFn = hmpl.compile(`{{#request src="/api/test"}}{{/request}}`, {
memo: true,
autoBody: { formData: true },
allowedContentTypes: ["text/html"],
disallowedTags: ["script"],
sanitize: false
});

The template is a string with HMPL markup (HTML + block helpers).

const template = `<div>{{#request src="/api/test"}}{{/request}}</div>`;
const templateFn = hmpl.compile(template);

The options argument establishes default behaviors for all requests generated by the compiled template function (except sanitizeConfig).

PropertyTypeDefaultDescription
memobooleanfalseEnables response caching for identical requests.
autoBodyboolean | HMPLAutoBodyOptionsfalseConfigures automatic request body generation.
allowedContentTypesHMPLContentTypes[“text/html”]Permitted response Content-Type headers.
sanitizeHMPLSanitizefalseEnables HTML sanitization of server responses.
sanitizeConfigConfig (from DOMPurify)undefinedConfiguration object for the sanitize function from DOMPurify.
disallowedTagsHMPLDisallowedTags[]HTML elements to remove from responses.
const options = {
memo: true,
autoBody: {
formData: true
},
allowedContentTypes: ["text/html"],
sanitize: true,
sanitizeConfig: { USE_PROFILES: { html: true } },
disallowedTags: ["iframe"]
};
const templateFn = hmpl.compile(template, options);

The sanitizeConfig property specifies configuration settings for the sanitize function of DOMPurify.

compile(`...`, {
sanitize: true,
sanitizeConfig: { USE_PROFILES: { html: true } }
});

Converts an HMPLRequestInfo object to a string containing the request.

ArgumentTypeDescriptionRequired
optionsHMPLRequestInfoRequest configuration interfaceYes
const info = hmpl.stringify({ src: "/api/test" });
// `{{#request src="/api/test"}}{{/request}}`
const templateFn = hmpl.compile(`${info}`);