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
).
Methods | Type | Description |
---|---|---|
compile | HMPLCompile | Transforms template strings into executable template functions |
stringify | (info: HMPLRequestInfo) => string | Serializes request configuration objects to component string |
import hmpl from "hmpl-js";const { compile, stringify } = hmpl;
compile
Section titled “compile”The compile
method processes HMPL template strings into executable template function.
Argument | Type | Description | Required |
---|---|---|---|
template | string | HMPL template string containing HTML and requests | Yes |
options | HMPLCompileOptions | Compilation configuration parameters | No |
const templateFn = hmpl.compile(`{{#request src="/api/test"}}{{/request}}`, { memo: true, autoBody: { formData: true }, allowedContentTypes: ["text/html"], disallowedTags: ["script"], sanitize: false});
template
Section titled “template”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);
options
Section titled “options”The options argument establishes default behaviors for all requests generated by the compiled template function.
Property | Type | Default | Description |
---|---|---|---|
memo | boolean | false | Enables response caching for identical requests. |
autoBody | boolean | HMPLAutoBodyOptions | false | Configures automatic request body generation. |
allowedContentTypes | HMPLContentTypes | [“text/html”] | Permitted response Content-Type headers. |
sanitize | HMPLSanitize | false | Enables HTML sanitization of server responses. |
disallowedTags | HMPLDisallowedTags | [] | 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);
stringify
Section titled “stringify”Converts an HMPLRequestInfo object
to a string
containing the request.
Argument | Type | Description | Required |
---|---|---|---|
options | HMPLRequestInfo | Request configuration interface | Yes |
const info = hmpl.stringify({ src: "/api/test" });// `{{#request src="/api/test"}}{{/request}}`const templateFn = hmpl.compile(`${info}`);