Skip to content
TwitterDiscordGitHub

Template Function

The template function instantiates template executions, managing server requests and DOM updates. Each invocation creates a unique instance.

AgumentTypeDescriptionRequired
optionsHMPLIdentificationRequestInit[] | HMPLRequestInit | HMPLRequestInitFunctionConfiguration for request initialization and executionNo
const instance1 = templateFn();
// Returns: { response: null, status: 403 }
const instance2 = templateFn({
credentials: "omit"
});
// Returns: { response: div, status: 200 }

The value of this argument is of three types:

This type describes the configuration for the fetch sent with each request in the template. The HMPLRequestInit interface extends the standard RequestInit with HMPL-specific properties.

PropertyTypeDescription
modeRequestModeThe mode of the request (cors, no-cors, same-origin)
cacheRequestCacheThe cache mode for the request
redirectRequestRedirectHow to handle redirects (follow, error, manual)
referrerPolicyReferrerPolicyPolicy for the referrer header
integritystringSubresource integrity value for the request
referrerstringThe referrer URL for the request
getHMPLRequestGetOptional function to retrieve properties from the request
bodyBodyInit | nullThe body of the request (can be a string, FormData, etc.)
signalAbortSignal | nullAn AbortSignal to abort the request if needed
windowanyReference to the window object (if applicable)
credentialsRequestCredentialsCredentials mode for the request
headersHMPLHeadersInitCustom headers for the request
timeoutnumberOptional timeout duration for the request in ms
const elementObj = templateFn({
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "text/html"
},
redirect: "follow",
get: (prop, value) => {},
referrerPolicy: "no-referrer",
body: JSON.stringify(data),
signal: new AbortController().signal,
integrity: "...",
window: null,
referrer: "about:client"
});

The get property specifies a callback function that executes on property updates within the HMPLInstance.

ArgumentTypeDescription
propstringThe name of the updated property
valueanyThe new property value
redirectHMPLRequestContextThe context of the current request sent to the HMPLInstance
requestHMPLRequestThe associated request block helper (for multi-request templates)
const elementObj = templateFn({
get: (prop, value, context, request) => {
switch (prop) {
case "response":
if (!request) console.log(request);
console.log("Response:");
console.log(value);
break;
case "status":
console.log("Status:");
console.log(value);
break;
}
}
});

Also, this function is great for implementing with Promise in JavaScript:

const val = await new Promise((res, rej) => {
templateFn({
get: (prop, value, context, request) => {
switch (prop) {
case "response":
if (!value) return;
res(value);
break;
}
}
});
});

This type is intended for the function for dynamic generation of RequestInit. This is necessary when, for example, event is processed.

ArgumentTypeDescription
contextHMPLInstanceContextExecution context containing request information
const elementObj = templateFn(({ request: { event, clearInterval } }) => {
clearInterval?.();
return {
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "text/html"
},
redirect: "follow",
get: (prop, value) => {},
referrerPolicy: "no-referrer",
body: new FormatData(event.target, event.submitter)
};
});

An array consisting of Identification RequestInit

Section titled “An array consisting of Identification RequestInit”

The last type is for cases where you want to specify which request RequestInit or the function that returns it applies to.

const instance = templateFn([
{
id: "1",
value: {
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "text/html"
},
redirect: "follow",
get: (prop, value) => {},
referrerPolicy: "no-referrer",
body: JSON.stringify(data)
}
}
]);

This object is necessary for associating RequestInit or the function that returns it with a specific request, since we specify the id in it. The relationship between Identification RequestInit and requests is one-to-many.

PropertyTypeDescriptionRequired
idstring | numberUnique identifier matching template request’s initIdYes
valueHMPLRequestInit | HMPLRequestInitFunctionThe initialization parameters for the referenced requestYes
const mySpecificInit = {
id: 1,
value: ({ request: { event } }) => {
return {
body: new FormatData(event.target, event.submitter)
};
}
};

and what the request itself looks like:

{{#request
src="/api/registration"
after="submit:form"
initId=1
}}{{/request}}