Skip to content

Commit

Permalink
Merge pull request #1 from github/initial-implementation
Browse files Browse the repository at this point in the history
Initial implementation
  • Loading branch information
keithamus authored Jul 31, 2020
2 parents bcec6d8 + 04e6506 commit 27e0df4
Show file tree
Hide file tree
Showing 18 changed files with 4,124 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"root": true,
"plugins": ["github"],
"extends": ["plugin:github/recommended", "plugin:github/typescript", "plugin:github/browser"],
"rules": {
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": ["error"]
},
"overrides": [
{
"files": "test/*",
"rules": {
"github/unescaped-html-literal": "off"
},
"globals": {
"chai": false,
"expect": false
},
"env": {
"mocha": true
}
},
{
"files": "*.cjs",
"env": {
"node": true
}
}
]
}
30 changes: 30 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build

on:
pull_request:
push:
branches:
- master

jobs:
test-node:
name: Test on Node.js
runs-on: ubuntu-latest
steps:
- name: Checkout the project
uses: actions/checkout@v2
- name: Cache node modules
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Use Node.js 14.7.0
uses: actions/setup-node@v1
with:
node-version: 14.7.0
- name: Install dependencies
run: npm i
- name: Run Node.js Tests
run: npm run test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
*.tsbuildinfo
lib/
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @github/web-systems-reviewers
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 GitHub, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Template Parts

This library is designed as a "ponyfill" library that implements the design in the [Template Parts Proposal](https://github.com/domenic/template-parts) that has been sketched out in order to address [whatwg/html#2254](https://github.com/whatwg/html/issues/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:

```js
import {StampedTemplate} from '@github/template-parts'

const tpl = new StampedTemplate(document.getElementById('foo'), (parts, params) => {
for (const part of parts) {
const key = part.expression.trim()
const value = (key in params ? params[key] : '')
if (part.attribute) {
part.value = value
} else {
part.replaceWith(value)
}
}
}, { x: 'Hello', y: 'bar'})
```

A `StampedTemplate` instance has the `fragment: DocumentFragment` property - the cloned contents of the template - and an `update(params: Record<string, unknown>): void` method - which when called will run the given processor again, with the new `params`.

This library also comes with two ready-to-use processors: `propertyIdentity` and `propertyIdentityOrBooleanAttribute`. These processors implement a basic functionality that will replace expressions with the corresponding property value in the params object. You can use this processors rather than writing a custom one:


```js
import {StampedTemplate, propertyIdentity, propertyIdentityOrBooleanAttribute} from '@github/template-parts'

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

// The `propertyIdentityOrBooleanAttribute` processor will check for `false`/`true` values which map to attribute values, and add/remove the attribute.
const tpl = new StampedTemplate(document.getElementById('foo'), propertyIdentityOrBooleanAttribute, { x: 'Hello', y: false})
```
Loading

0 comments on commit 27e0df4

Please sign in to comment.