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.

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.
disallowedTagsHMPLDisallowedTags[]HTML elements to remove from responses.
const options = {
memo: true,
autoBody: {
formData: true
},
allowedContentTypes: ["text/html"],
sanitize: true,
disallowedTags: ["iframe"]
};
const templateFn = hmpl.compile(template, options);

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}`);