TemplateUtils is a utility class for working with HTML templates in JavaScript or TypeScript projects.
To install TemplateUtils, you can use npm or yarn:
npm install @oleksii-pavlov/templates
# or
yarn add @oleksii-pavlov/templates
Import TemplateUtils in your TypeScript or JavaScript file:
import { TemplateUtils } from '@oleksii-pavlov/templates';
Fills the innerHTML of elements matching the selector with the provided content.
Mounts the template string into the specified root element.
Replaces elements matching the selector with the provided content.
Concatenates an array of template strings into a single string.
hydrateTemplates(selector: string, callback: (element: Element, index: number, array: Element[]) => void): void
Hydrates templates matching the given selector with a callback function.
Returns an array of elements matching the given selector.
Returns an array of elements matching the given attributes.
Maps attributes to an HTML string.
import { TemplateUtils } from '@oleksii-pavlov/templates';
// Example usage
TemplateUtils.fillTemplate('.container', '<h1>Hello, world!</h1>');
const root = document.getElementById('root');
const template = '<div><p>This is a template</p></div>';
TemplateUtils.mountTemplate(root, template);