0.2.2 โ€ข Published 3 years ago

@elementumjs/template v0.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

CDN package_version production develop reference license

@elementum/template is a lightweight and powerful HTML template engine for vanilla WebComponents.


How to use it

Creating a basic template: html function

To define and init a new Template, you need to use the html template tag:

    import { html, render } from '@elementumjs/template';

    const template = (list) => html`<div>
            <p>My list has ${ list.length } item(s).</p>
        </div>`;

Nested templates

It's also possible define nested templates to create more complex elements. It is useful for render lists or conditionals:

  • Basic template: Include it into the html representation as other value using the html tag.
        const template = () => html`<p>Random number: ${ html`<b>${ Math.random() }</b>` }</p>`;
  • Conditional rendering: Return a template based on a condition:

        const userProfile = (user) => html`...`;
        const loginButton = () => html`...`;
    
        const template = (userLogged) => html`<div>
                ${ user !== undefined ? userProfile(user) : loginButton() }
            </div>`;
  • List of templates

        const listTemplate = (list) => html`<ul>
                ${ list.map(item => html`<li>${ item }</li>` ) }
            </ul>`;

Following the example...

    import { html, render } from '@elementumjs/template';

    const listTemplate = (list) => html`<ul>
            ${ list.map(item => html`<li>${ item }</li>` ) }
        </ul>`;

    const template = (list) => html`<div>
            <p>My list has ${ list.length } item(s).</p>
            ${ listTemplate(list) }
        </div>`;

Rendering into a container

To render the template into a container HTMLElement, the data to fill the template is passed as an attribute to the template generator function. The result of that function will be parsed by render function to check if the template is already rendered and update it or is not rendered yet and inject it.

    import { html, render } from '@elementumjs/template';

    // const listTemplate = ...;
    // const template = ...;

    const list = [ "item 1" ];
    render(template(list), document.body /* the container to render the template */);

Full example

    // import { html, render } from "https://cdn.jsdelivr.net/gh/elementumjs/template/dist/template.esm.js";
    import { html, render } from '@elementumjs/template';

    // Create the templates
    const listTemplate = (list) => html`<ul>
            ${ list.map(item => html`<li>${ item }</li>` ) }
        </ul>`;

    const template = (list) => html`<div>
            <p>My list has ${ list.length } item(s).</p>
            ${ listTemplate(list) }
        </div>`;

    // Instance the list and render the template into the container.
    const list = [ "item 1" ];
    render(template(list), document.body);

    // Update the list and re-render the template every second
    let loop = setInterval(() => {
        list.push(`item ${list.length + 1}`)
        render(template(list), document.body);

        if (counter == 5) clearInterval(loop);
    }, 1000);

Installation

Import from CDN as ES Module

Import from jsDelivr CDN:

    import { html, render } from "https://cdn.jsdelivr.net/gh/elementumjs/template/dist/template.esm.js";

Or install the package locally

Download the package

Install via npm:

    npm install @elementumjs/template
Import as ES Module

ES Module builds are intended for use with modern bundlers like webpack 2 or rollup. Use it with ES6 JavaScript import:

    import { html, render } from '@elementumjs/template';

Other import methods

Checkout other import methods in dist/README.md.

0.2.1

3 years ago

0.2.2

3 years ago

0.2.0

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago