TemplateManager
can be used when working with custom elements.
It was created while keeping in mind the functionality that was used in first
electron-element
send-feedback. It provides
basic templating utilities.
Throughout this docs templates
will be:
const { TemplateManager } = require('@electron-elements/utils');
const templates = new TemplateManager();
name
(string) - the name of template you are addinghtml
(string) - html of the template- data (object: optional) - the data for the template bindings
You can add multiple templates, which allows you to have organized templates for the different parts of your element. The data and template bindings are case sensitive.
Example:
const data = { name: 'utils' };
templates.add('first-template', `
<div>Hello, {{ name }}</div>
`, data)
name
(string) - the name of the template to updatedata
(object) - data to update the template bindings withnoCache
(boolean: optional, default: false) - weather to use cached data
This method can be used to update the templates. You can update a pre-existing template, use cached data so you don't have to provide old template binding data:
// update the previously added template
// to have `<div>Hello, Utils</div>`
templates.update('first-template', { name: 'Utils' });
// example of caching
templates.add('second-template', `
first key: {{ key-one }}
second key: {{ key-two }}
`, { 'key-one': '<First Key>' });
/* now the template would be:
first key: <First Key>
second key: {{ key-two }}
*/
// to update the second key without having
// to provide the first key
templates.update('second-template', { 'second-key': '<Second key>' })
/* now the template would be
first key: <First Key>
second key: <Second Key>
*/
// To disable this behavoir you can use noCache option
templates.update('second-template', { 'key-one': 'First Key' }, true);
/* now the template would be
first key: First Key
second key: {{ key-two }}
*/
name
(string) - name of the template
returns true
if the template is added otherwise false
.
name
(string) - name of the template
returns DocumentFragment
of the template, internally <template>
element is
used to keep track and update the template so the DOM is pre-parsed.
name
(string) - name of the template
returns string
of the html template you passed in.
name
(string) - name of the template
returns object
of the cached data for template.
name
(string) - name of the template
Deletes the cached data for that template.
name
(string) - the name of templatehtml
(string) - the html of templatedata
(object: optional) - the data for template bindings
To update the whole template's html you can use this method.
name
- the name of the template to remove
Remove the templates from TemplateManager