@ramstack/alpinegear-template 1.0.0-preview.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -0
- package/alpinegear-template.esm.js +30 -0
- package/alpinegear-template.esm.min.js +1 -0
- package/alpinegear-template.js +35 -0
- package/alpinegear-template.min.js +1 -0
- package/package.json +18 -0
package/README.md
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
# @ramstack/alpinegear-template
|
2
|
+
|
3
|
+
`@ramstack/alpinegear-template` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-template` directive.
|
4
|
+
|
5
|
+
This directive allows you to define a template once anywhere in the DOM and reference it by its ID.
|
6
|
+
|
7
|
+
This helps avoid duplicating templates, simplifying markup and making it easier to manage.
|
8
|
+
|
9
|
+
Moreover, it enables recursive templates, allowing you to create components like a **tree view** with ease,
|
10
|
+
something that would otherwise be quite complex to implement.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
### Using CDN
|
15
|
+
To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
|
16
|
+
|
17
|
+
```html
|
18
|
+
<!-- alpine.js plugin -->
|
19
|
+
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-template@1/alpinegear-template.min.js" defer></script>
|
20
|
+
|
21
|
+
<!-- alpine.js -->
|
22
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
23
|
+
```
|
24
|
+
|
25
|
+
### Using NPM
|
26
|
+
Alternatively, you can install the plugin via `npm`:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
npm install --save @ramstack/alpinegear-template
|
30
|
+
```
|
31
|
+
|
32
|
+
Then initialize it in your bundle:
|
33
|
+
|
34
|
+
```js
|
35
|
+
import Alpine from "alpinejs";
|
36
|
+
import template from "@ramstack/alpinegear-template";
|
37
|
+
|
38
|
+
Alpine.plugin(template);
|
39
|
+
Alpine.start();
|
40
|
+
```
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
Here's a simple example where the template definition is separated from the main markup:
|
44
|
+
|
45
|
+
```html
|
46
|
+
<template id="columns-template">
|
47
|
+
<td x-text="item.id"></td>
|
48
|
+
<td x-text="item.name"></td>
|
49
|
+
<td x-text="item.description"></td>
|
50
|
+
</template>
|
51
|
+
|
52
|
+
<div x-data="{
|
53
|
+
items: [
|
54
|
+
{ id: 1, name: 'Item 1', description: 'Description 1' },
|
55
|
+
{ id: 2, name: 'Item 2', description: 'Description 2' },
|
56
|
+
{ id: 3, name: 'Item 3', description: 'Description 3' } ]
|
57
|
+
}">
|
58
|
+
<table>
|
59
|
+
<template x-for="item in items" :key="item.id">
|
60
|
+
<tr x-template="columns-template"></tr>
|
61
|
+
</template>
|
62
|
+
</table>
|
63
|
+
</div>
|
64
|
+
```
|
65
|
+
|
66
|
+
In this example, the table column template is extracted into a separate template (`columns-template`),
|
67
|
+
which is referenced inside the loop.
|
68
|
+
|
69
|
+
Note that the template uses the data context available at the point where it's referenced,
|
70
|
+
as if it was defined in place.
|
71
|
+
|
72
|
+
> [!TIP]
|
73
|
+
> You can use multiple root elements inside a given template.
|
74
|
+
|
75
|
+
> [!IMPORTANT]
|
76
|
+
> * Templates should be defined using the `<template>` tag with a unique ID.
|
77
|
+
> * The `x-template` directive can only be applied to non-`<template>` elements.
|
78
|
+
|
79
|
+
### Recursive Template
|
80
|
+
|
81
|
+
Since you can reference a template within itself by ID, it becomes easy to render **tree-like** structures -
|
82
|
+
an otherwise challenging task.
|
83
|
+
|
84
|
+
Here's an example of rendering a simple file tree using `<ul>` tags:
|
85
|
+
|
86
|
+
```html
|
87
|
+
<template id="treeitem">
|
88
|
+
<span x-format>{{ model.name }}</span>
|
89
|
+
|
90
|
+
<template x-if="model.list">
|
91
|
+
<ul>
|
92
|
+
<template x-for="item in model.list">
|
93
|
+
<li x-template="treeitem" x-data="{ model: item }"></li>
|
94
|
+
</template>
|
95
|
+
</ul>
|
96
|
+
</template>
|
97
|
+
</template>
|
98
|
+
|
99
|
+
<ul x-data="json">
|
100
|
+
<li x-template="treeitem"></li>
|
101
|
+
</ul>
|
102
|
+
```
|
103
|
+
```js
|
104
|
+
const json = {
|
105
|
+
model: {
|
106
|
+
name: 'root',
|
107
|
+
list: [
|
108
|
+
{
|
109
|
+
name: 'Documents',
|
110
|
+
list: [
|
111
|
+
{ name: 'Resume.docx' },
|
112
|
+
{ name: 'CoverLetter.docx' }
|
113
|
+
]
|
114
|
+
},
|
115
|
+
{
|
116
|
+
name: 'Pictures',
|
117
|
+
list: [
|
118
|
+
{
|
119
|
+
name: 'Nature',
|
120
|
+
list: [
|
121
|
+
{ name: 'Mountains.jpg' },
|
122
|
+
{ name: 'River.jpg' }
|
123
|
+
]
|
124
|
+
}
|
125
|
+
]
|
126
|
+
}
|
127
|
+
]
|
128
|
+
}
|
129
|
+
};
|
130
|
+
```
|
131
|
+
|
132
|
+
This will generate the following HTML structure:
|
133
|
+
|
134
|
+
* root
|
135
|
+
* Documents
|
136
|
+
* Resume.docx
|
137
|
+
* CoverLetter.docx
|
138
|
+
* Pictures
|
139
|
+
* Nature
|
140
|
+
* Mountains.jpg
|
141
|
+
* River.jpg
|
142
|
+
|
143
|
+
As you can see, we are able to render nested elements by recursively referencing the same template within itself,
|
144
|
+
which opens up a lot of possibilities for complex layouts.
|
145
|
+
|
146
|
+
## Source code
|
147
|
+
You can find the source code for this plugin on GitHub:
|
148
|
+
|
149
|
+
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/template
|
150
|
+
|
151
|
+
## Contributions
|
152
|
+
Bug reports and contributions are welcome.
|
153
|
+
|
154
|
+
## License
|
155
|
+
This package is released as open source under the **MIT License**.
|
156
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
const warn = (...args) => console.warn("alpine-gear.js:", ...args);
|
2
|
+
const is_template = el => el instanceof HTMLTemplateElement;
|
3
|
+
|
4
|
+
function plugin(alpine) {
|
5
|
+
alpine.directive("template", (el, { expression }) => {
|
6
|
+
if (is_template(el)) {
|
7
|
+
warn("x-template cannot be used on a 'template' tag.");
|
8
|
+
return;
|
9
|
+
}
|
10
|
+
|
11
|
+
const tpl = document.getElementById(expression);
|
12
|
+
|
13
|
+
if (!is_template(tpl)) {
|
14
|
+
warn("x-template directive can only reference the template tag.");
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
queueMicrotask(() => {
|
24
|
+
el.innerHTML = "";
|
25
|
+
el.append(tpl.content.cloneNode(true));
|
26
|
+
});
|
27
|
+
});
|
28
|
+
}
|
29
|
+
|
30
|
+
export { plugin as template };
|
@@ -0,0 +1 @@
|
|
1
|
+
const e=(...e)=>console.warn("alpine-gear.js:",...e),t=e=>e instanceof HTMLTemplateElement;function n(n){n.directive("template",((n,{expression:a})=>{if(t(n))return void e("x-template cannot be used on a 'template' tag.");const o=document.getElementById(a);t(o)?queueMicrotask((()=>{n.innerHTML="",n.append(o.content.cloneNode(!0))})):e("x-template directive can only reference the template tag.")}))}export{n as template};
|
@@ -0,0 +1,35 @@
|
|
1
|
+
(function () {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
const warn = (...args) => console.warn("alpine-gear.js:", ...args);
|
5
|
+
const is_template = el => el instanceof HTMLTemplateElement;
|
6
|
+
|
7
|
+
function plugin(alpine) {
|
8
|
+
alpine.directive("template", (el, { expression }) => {
|
9
|
+
if (is_template(el)) {
|
10
|
+
warn("x-template cannot be used on a 'template' tag.");
|
11
|
+
return;
|
12
|
+
}
|
13
|
+
|
14
|
+
const tpl = document.getElementById(expression);
|
15
|
+
|
16
|
+
if (!is_template(tpl)) {
|
17
|
+
warn("x-template directive can only reference the template tag.");
|
18
|
+
return;
|
19
|
+
}
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
queueMicrotask(() => {
|
27
|
+
el.innerHTML = "";
|
28
|
+
el.append(tpl.content.cloneNode(true));
|
29
|
+
});
|
30
|
+
});
|
31
|
+
}
|
32
|
+
|
33
|
+
document.addEventListener("alpine:init", () => { Alpine.plugin(plugin); });
|
34
|
+
|
35
|
+
})();
|
@@ -0,0 +1 @@
|
|
1
|
+
!function(){"use strict";const e=(...e)=>console.warn("alpine-gear.js:",...e),t=e=>e instanceof HTMLTemplateElement;function n(n){n.directive("template",((n,{expression:i})=>{if(t(n))return void e("x-template cannot be used on a 'template' tag.");const a=document.getElementById(i);t(a)?queueMicrotask((()=>{n.innerHTML="",n.append(a.content.cloneNode(!0))})):e("x-template directive can only reference the template tag.")}))}document.addEventListener("alpine:init",(()=>{Alpine.plugin(n)}))}();
|
package/package.json
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"name": "@ramstack/alpinegear-template",
|
3
|
+
"version": "1.0.0-preview.1",
|
4
|
+
"description": "@ramstack/alpinegear-template provides 'x-template' Alpine.js directive, that enhances template management, providing better control over reusable template blocks, dynamic and recursive component rendering.",
|
5
|
+
"author": "Rameel Burhan",
|
6
|
+
"license": "MIT",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "git+https://github.com/rameel/ramstack.alpinegear.js.git",
|
10
|
+
"directory": "src/plugins/template"
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"alpine.js",
|
14
|
+
"alpinejs"
|
15
|
+
],
|
16
|
+
"main": "alpinegear-template.js",
|
17
|
+
"module": "alpinegear-template.esm.js"
|
18
|
+
}
|