@github/template-parts

中文文档
0.7.1发布于 2024/12/15MITTypeScriptES Module零依赖近 30 天下载 8735
原始使用文档

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)

版本与发布记录

@github/template-parts 共发布 23 个版本,首次发布于 2020/07/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.7.1latest2024/12/151.9 年前
0.7.02024/12/131.9 年前
0.6.32024/12/131.9 年前
0.6.22024/12/091.9 年前
0.6.12024/12/031.9 年前
0.6.02024/12/032 年前

关于 @github/template-parts 的常见问题

@github/template-parts 是什么?

An implementation of the TemplateInstance proposal (https://github.com/w3c/webcomponents/blob/159b1600bab02fe9cd794825440a98537d53b389/proposals/Template-Instantiation.md)

如何安装 @github/template-parts?

在项目目录运行 npm install @github/template-parts,或使用 pnpm add / yarn add / bun add。

@github/template-parts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@github/template-parts 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@github/template-parts 可以商用吗?

@github/template-parts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@github/template-parts 的下载量有多少?

根据 npm 官方下载统计,@github/template-parts 在 2026/10/17 – 2026/11/15 这 30 天内被下载 8,735 次,日均约 291 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@github/template-parts 还在维护吗?

最新版本 0.7.1 发布于 2024/12/15(1.9 年前);至今共发布 23 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/07/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。