npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bound-template

v0.1.0

Published

A micro-library for inserting data into HTML Templates via named bindings

Downloads

5

Readme

BoundTemplate

A micro-library for binding data to HTML Templates.

Goals

This library only aims to accomplish three things:

  1. Provide a simple method of inserting data in HTML Templates. This means no logic and no two-way bindings.
  2. Efficiently update bound data by operating on the smallest number of Nodes possible.
  3. Do 1 & 2 with as little code as possible while keeping the source code clear & readable.

That's it. The goals may change in the future, but for now the overarching theme is simplicity and doing one thing (data insertion) well.

Syntax

The syntax to bind a value to the DOM is simple: {{name}}. You can bind to element attributes as well as text nodes.

<template id="greeting">
  <p class="{{color}} greeting">Hello, {{name}}!</p>
</template>

You can bind multiple values as well:

<template id="greeting">
  <p class="{{color}} {{type}}">{{greeting}}, {{name}}!</p>
</template>

Both text nodes and element attributes are coerced to string values before being inserted into the DOM. While this is obvious for text nodes, it is important to note this behavior for attributes as well (which can only return null or string values).

If you want to bind non-string values, then you can bind directly to an element's properties by appending $ to an attribute name:

<p my-prop$={{data}}></p>

Properties will not appear in the DOM, but instead are set on a props property on the specified element. In other words, after rendering the above you might get:

console.log(document.querySelector('p').props.data); // Logs the value of `data`, not coerced.

The special syntax makes it obvious that the value will be applied to a property and not an attribute. Note that you cannot apply more than one binding to a property. If more than one is specified, the subsequent bindings will simply be ignored.

Finally, you can also bind event handlers using the on- prefix:

<p on-click={{clickHandler}}></p>

Anytime the binding for an event handler is updated, the old handler will be removed and the new one attached.

Usage

The API surface is relatively small and thus simple to use.

To get started, simply pass a reference to a template you would like to bind into the BoundTemplate constructor (the only export from this package):

const template = document.getElementById('greeting');
const boundTemplate = new BoundTemplate(template);

The resulting boundTemplate object is a factory function with only one method, create():

const [instance, bindings] = boundTemplate.create();

create() will return a cloned instance of the template, which you can then insert into the DOM, and a bindings object which allows you to set values directly into the template instance:

// Insert template instance into DOM
component.shadowRoot.appendChild(instance);

// Update data in the DOM
bindings.set('color', 'red');
bindings.set('name', 'Zelda');

You can optionally pass an object to create which will be used to instantiate the bindings to default values:

const [instance, bindings] = boundTemplate.create({
  color: 'red',
  name: 'Zelda'
});

Similarly, you can set multiple values at a time by passing an object to the setData function:

bindings.setData({
  color: 'red',
  name: 'Zelda'
});

For further examples, see the Examples folder.