An implementation of the Template Parts proposal
Перейти к файлу
Keith Cirkel e4ef63635b
Merge pull request #72 from seanpdoyle/span-template-boundary
Render across `<template>` element boundaries
2024-11-11 09:28:22 +00:00
.github upgrade nodejs build job 2022-04-14 12:41:20 +01:00
examples docs: cleanup example.html 2020-08-25 10:14:09 +01:00
src Render across `<template>` element boundaries 2024-11-08 15:10:44 -05:00
test Render across `<template>` element boundaries 2024-11-08 15:10:44 -05:00
.eslintrc.json update all deps & clean up code 2024-11-04 10:49:12 +00:00
.gitignore switch to @web/test-runner 2022-04-14 12:37:11 +01:00
CODEOWNERS move AOR to primer 2022-09-23 17:57:00 +01:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2020-09-17 17:13:47 +01:00
CONTRIBUTING.md Create CONTRIBUTING.md 2020-09-17 17:13:23 +01:00
LICENSE Update LICENSE 2020-07-31 11:06:58 +01:00
README.md docs: fix broken examples 2020-08-14 11:52:35 +01:00
SECURITY.md Create SECURITY.md 2020-09-17 12:14:53 -04:00
package-lock.json update all deps & clean up code 2024-11-04 10:49:12 +00:00
package.json update all deps & clean up code 2024-11-04 10:49:12 +00:00
tsconfig.build.json fix tsconfig.build.json test path 2022-05-11 20:08:22 +00:00
tsconfig.json update all deps & clean up code 2024-11-04 10:49:12 +00:00
web-test-runner.config.js update all deps & clean up code 2024-11-04 10:49:12 +00:00

README.md

Template Parts

This library is designed as a "ponyfill" library that implements the design in the TemplateInstance proposed whatwg spec that has been proposed in order to address whatwg/html#2254.

This implements the minimally viable parts of the proposal, to provide something that works, but should be easy to drop if the Template Parts Proposal lands.

To reiterate the example in the above proposal, given a template such as:

<template id="foo">
  <div class="foo {{y}}">{{x}} world</div>
</template>

We'd like {{x}} and {{y}} to be template parts, exposed as JavaScript objects which can be manipulated.

With this library, and that given template, one could implement the following:

import {TemplateInstance} from '@github/template-parts'

const tpl = new TemplateInstance(document.getElementById('some-template'), { x: 'Hello', y: 'bar'})

document.appendChild(tpl)

A TemplateInstance instance is a subclass of DocumentFragment - containing the cloned contents of the template. It also has an update(params: unknown): void method - which when called will run the given "processor", with the new params.

This library has a default "processor": propertyIdentity which implements basic functionality of applying the params object values to the Template Parts (it is effectively part.value = params[part.expression])

In addition, there is a propertyIdentityOrBooleanAttribute export which adds the capability of toggling boolean style attributes like hidden or input.required.

To use the propertyIdentityOrBooleanAttribute, import it and pass it as a third argument to the TemplateInstance constructor:

import {TemplateInstance, propertyIdentityOrBooleanAttribute} from '@github/template-parts'

// This will simply replace `{{x}}` with `"Hello"` and `{{y}}` with `"bar"`
const tpl = new TemplateInstance(document.getElementById('foo'), { x: 'Hello', y: 'bar'})

// The `propertyIdentityOrBooleanAttribute` processor will check for `false`/`true` values which map to Template Part values that are assigned to attributes, and add/remove the attribute.
const tpl = new TemplateInstance(document.getElementById('foo'), { x: 'Hello', hidden: false}, propertyIdentityOrBooleanAttribute)