zzz-template 0.9.1 → 1.0.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 +25 -25
- package/fs.d.ts +4 -4
- package/index.d.ts +8 -8
- package/index.js +6 -3
- package/{fs.js → node.js} +5 -2
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -39,9 +39,9 @@ npm install zzz-template
|
|
|
39
39
|
### Compile example (see [examples/00-compile](examples/00-compile))
|
|
40
40
|
``` javascript
|
|
41
41
|
// file examples/00-compile/example.js
|
|
42
|
-
import {
|
|
42
|
+
import {ZzzTemplate} from "zzz-template"
|
|
43
43
|
|
|
44
|
-
const zzz = new
|
|
44
|
+
const zzz = new ZzzTemplate()
|
|
45
45
|
const fn = zzz.compile('Hello ${data.name}') // returns function that renders your template using data: `fn(data)`
|
|
46
46
|
console.log(fn({name: 'Jerry'})); // > "Hello Jerry"
|
|
47
47
|
console.log(fn({name: 'Tom'})); // > "Hello Tom"
|
|
@@ -57,9 +57,9 @@ console.log(fn({name: 'Tom'})); // > "Hello Tom"
|
|
|
57
57
|
</script>
|
|
58
58
|
|
|
59
59
|
<script type="module">
|
|
60
|
-
import {
|
|
60
|
+
import { ZzzTemplate } from '/zzz-template/index.js'
|
|
61
61
|
|
|
62
|
-
const zzz = new
|
|
62
|
+
const zzz = new ZzzTemplate()
|
|
63
63
|
|
|
64
64
|
const result = zzz.render('template', { name: 'world' })
|
|
65
65
|
|
|
@@ -78,9 +78,9 @@ console.log(fn({name: 'Tom'})); // > "Hello Tom"
|
|
|
78
78
|
```
|
|
79
79
|
``` javascript
|
|
80
80
|
// file examples/02-basic/example.js
|
|
81
|
-
import {
|
|
81
|
+
import { ZzzTemplateNode } from 'zzz-template/node.js'
|
|
82
82
|
|
|
83
|
-
const zzz = new
|
|
83
|
+
const zzz = new ZzzTemplateNode({ dir: import.meta.dirname })
|
|
84
84
|
const result = zzz.render('template.html', { name: 'Jerry' })
|
|
85
85
|
console.log(result)
|
|
86
86
|
// OUTPUT:
|
|
@@ -97,9 +97,9 @@ console.log(result)
|
|
|
97
97
|
``` html
|
|
98
98
|
<!-- file examples/03-include/page.html -->
|
|
99
99
|
<script type="module">
|
|
100
|
-
import {
|
|
100
|
+
import { ZzzTemplate, useInclude } from '/zzz-template/index.js'
|
|
101
101
|
|
|
102
|
-
const zzz = new
|
|
102
|
+
const zzz = new ZzzTemplate()
|
|
103
103
|
useInclude(zzz) // 👈 enables include feature
|
|
104
104
|
|
|
105
105
|
const result = zzz.render('template', { name: 'world' })
|
|
@@ -131,9 +131,9 @@ console.log(result)
|
|
|
131
131
|
``` html
|
|
132
132
|
<!-- file examples/06-layout/layouts.html -->
|
|
133
133
|
<script type="module">
|
|
134
|
-
import {
|
|
134
|
+
import { ZzzTemplate, useLayout } from '/zzz-template/index.js'
|
|
135
135
|
|
|
136
|
-
const zzz = new
|
|
136
|
+
const zzz = new ZzzTemplate()
|
|
137
137
|
useLayout(zzz) // 👈 enables layout feature
|
|
138
138
|
|
|
139
139
|
const result = zzz.render('template', { name: 'world' })
|
|
@@ -195,9 +195,9 @@ See example how layout can include each other: `examples/06-layout/layouts2.html
|
|
|
195
195
|
</script>
|
|
196
196
|
|
|
197
197
|
<script type="module">
|
|
198
|
-
import {
|
|
198
|
+
import { ZzzTemplate, useLayout, useLocal } from '/zzz-template/index.js'
|
|
199
199
|
|
|
200
|
-
const zzz = new
|
|
200
|
+
const zzz = new ZzzTemplate()
|
|
201
201
|
useLayout(zzz) // 👈 enables layout feature
|
|
202
202
|
useLocal(zzz) // 👈 enables local vars feature
|
|
203
203
|
|
|
@@ -218,9 +218,9 @@ See example how layout can include each other: `examples/06-layout/layouts2.html
|
|
|
218
218
|
``` html
|
|
219
219
|
<!-- file examples/04-if/if.html -->
|
|
220
220
|
<script type="module">
|
|
221
|
-
import {
|
|
221
|
+
import { ZzzTemplate, useIfMap } from '/zzz-template/index.js'
|
|
222
222
|
|
|
223
|
-
const zzz = new
|
|
223
|
+
const zzz = new ZzzTemplate()
|
|
224
224
|
useIfMap(zzz) // 👈 enables "if/map" feature
|
|
225
225
|
|
|
226
226
|
const result = zzz.render('template', { name: 'world', n: 42 })
|
|
@@ -255,9 +255,9 @@ See example how layout can include each other: `examples/06-layout/layouts2.html
|
|
|
255
255
|
``` html
|
|
256
256
|
<!-- file examples/04-if/ifi.html -->
|
|
257
257
|
<script type="module">
|
|
258
|
-
import {
|
|
258
|
+
import { ZzzTemplate, useIfMap } from '/zzz-template/index.js'
|
|
259
259
|
|
|
260
|
-
const zzz = new
|
|
260
|
+
const zzz = new ZzzTemplate()
|
|
261
261
|
useIfMap(zzz) // 👈 enables "if/map" feature
|
|
262
262
|
|
|
263
263
|
const result = zzz.render('template', { name: 'world', n: 42 })
|
|
@@ -288,9 +288,9 @@ See example how layout can include each other: `examples/06-layout/layouts2.html
|
|
|
288
288
|
``` html
|
|
289
289
|
<!-- file examples/05-map/map.html -->
|
|
290
290
|
<script type="module">
|
|
291
|
-
import {
|
|
291
|
+
import { ZzzTemplate, useIfMap } from '/zzz-template/index.js'
|
|
292
292
|
|
|
293
|
-
const zzz = new
|
|
293
|
+
const zzz = new ZzzTemplate()
|
|
294
294
|
useIfMap(zzz) // 👈 enables "if/map" feature
|
|
295
295
|
|
|
296
296
|
const pets = [{ name: 'cat', say: 'meow' }, { name: 'dog', say: 'woof' }]
|
|
@@ -328,9 +328,9 @@ ZzzTemplate already has a few built-in plugins. A plugin is just a function that
|
|
|
328
328
|
For instance, you can inject your code into the compile function. Here is a 'trim' example:
|
|
329
329
|
|
|
330
330
|
``` javascript
|
|
331
|
-
import {
|
|
331
|
+
import {ZzzTemplate, useContentTrim} from "zzz-template"
|
|
332
332
|
|
|
333
|
-
const zzz = new
|
|
333
|
+
const zzz = new ZzzTemplate()
|
|
334
334
|
useContentTrim(zzz)
|
|
335
335
|
const fn = zzz.compile(' Hello ${data.name} ')
|
|
336
336
|
// note that result is trimmed
|
|
@@ -349,9 +349,9 @@ This function pushes a code snippet to the end array that will be invoked after
|
|
|
349
349
|
Or you may want to introduce a new var in your templates.
|
|
350
350
|
|
|
351
351
|
``` javascript
|
|
352
|
-
import {
|
|
352
|
+
import {ZzzTemplate, useContentTrim} from "zzz-template"
|
|
353
353
|
|
|
354
|
-
const zzz = new
|
|
354
|
+
const zzz = new ZzzTemplate()
|
|
355
355
|
|
|
356
356
|
// zzz.s -- s means start (before template content compiled)
|
|
357
357
|
zzz.s.push('let $$ = data;') // introduce `$$` for `data`
|
|
@@ -366,9 +366,9 @@ console.log(result); // > "Hello Tom"
|
|
|
366
366
|
``` html
|
|
367
367
|
<!-- file examples/10-extend/escape.html -->
|
|
368
368
|
<script type="module">
|
|
369
|
-
import {
|
|
369
|
+
import { ZzzTemplate, useFn } from '/zzz-template/index.js'
|
|
370
370
|
|
|
371
|
-
const zzz = new
|
|
371
|
+
const zzz = new ZzzTemplate()
|
|
372
372
|
|
|
373
373
|
function escapeHtml (unsafe) {
|
|
374
374
|
return unsafe
|
|
@@ -441,4 +441,4 @@ Looking for JavaScript template engines? Here are some alternatives:
|
|
|
441
441
|
- [doT](https://www.npmjs.com/package/dot) - Fast template engine
|
|
442
442
|
|
|
443
443
|
---
|
|
444
|
-
Docs revision: 2025-
|
|
444
|
+
Docs revision: 2025-12-01T07:31:03.778Z
|
package/fs.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ZzzTemplateBase } from "./index.js";
|
|
2
2
|
|
|
3
3
|
export * from './index.js';
|
|
4
4
|
|
|
5
|
-
export interface
|
|
5
|
+
export interface ZzzTemplateNodeOptions extends Record<string, any> {
|
|
6
6
|
dir?: string;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export class
|
|
9
|
+
export class ZzzTemplateNode extends ZzzTemplateBase {
|
|
10
10
|
_dir: string;
|
|
11
|
-
constructor($this?:
|
|
11
|
+
constructor($this?: ZzzTemplateNodeOptions);
|
|
12
12
|
read(f: string): string;
|
|
13
13
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export class
|
|
1
|
+
export class ZzzTemplateBase {
|
|
2
2
|
s: string[];
|
|
3
3
|
e: string[];
|
|
4
4
|
$: Record<string, any>;
|
|
@@ -8,13 +8,13 @@ export class ZzzBase {
|
|
|
8
8
|
read(f: string): string;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export class
|
|
11
|
+
export class ZzzTemplate extends ZzzTemplateBase {
|
|
12
12
|
read(f: string): string;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export function useFn(zzz:
|
|
16
|
-
export function useContentTrim(zzz:
|
|
17
|
-
export function useInclude(zzz:
|
|
18
|
-
export function useLayout(zzz:
|
|
19
|
-
export function useLocal(zzz:
|
|
20
|
-
export function useIfMap(zzz:
|
|
15
|
+
export function useFn(zzz: ZzzTemplateBase, fn: Function, alias?: string | false): void;
|
|
16
|
+
export function useContentTrim(zzz: ZzzTemplateBase): void;
|
|
17
|
+
export function useInclude(zzz: ZzzTemplateBase, alias?: string): void;
|
|
18
|
+
export function useLayout(zzz: ZzzTemplateBase, alias?: string): void;
|
|
19
|
+
export function useLocal(zzz: ZzzTemplateBase, aliasSet?: string, aliasSeta?: string): void;
|
|
20
|
+
export function useIfMap(zzz: ZzzTemplateBase, aliases?: boolean): void;
|
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export class
|
|
1
|
+
export class ZzzTemplateBase {
|
|
2
2
|
s = [] // s - start, before content
|
|
3
3
|
e = [] // e - end, after content
|
|
4
4
|
constructor($this = {}) {
|
|
5
5
|
this.$ = $this
|
|
6
6
|
}
|
|
7
7
|
compile(str, local, sign = 'data, parent') {
|
|
8
|
-
const fn = new Function(sign, `${this.s.join(';')}
|
|
8
|
+
const fn = new Function(sign, `${this.s.join(';')}var content=\`${str}\`;${this.e.join(';')}return content;`)
|
|
9
9
|
return fn.bind({...this.$, local}) // shallow copy of $
|
|
10
10
|
}
|
|
11
11
|
render(template, data, local = {}) {
|
|
@@ -13,7 +13,7 @@ export class ZzzBase {
|
|
|
13
13
|
}
|
|
14
14
|
read(f) {} // must be implemented
|
|
15
15
|
}
|
|
16
|
-
export class
|
|
16
|
+
export class ZzzTemplate extends ZzzTemplateBase {
|
|
17
17
|
read (f) {
|
|
18
18
|
return window.document.getElementById(f).innerText
|
|
19
19
|
}
|
|
@@ -47,3 +47,6 @@ export function useIfMap(zzz, aliases = true) {
|
|
|
47
47
|
useFn(zzz, function map_template(arr, str) {return arr.map(x => {return zzz.compile(str, this.local)(x)}).join('')}, aliases && 'MAP')
|
|
48
48
|
useFn(zzz, function map_include(arr, file) {return arr.map(x => {return this.include(file, x)}).join('')}, aliases && 'MAPI')
|
|
49
49
|
}
|
|
50
|
+
|
|
51
|
+
// @deprecated ZzzBrowser, use ZzzTemplate class instead
|
|
52
|
+
export const ZzzBrowser = ZzzTemplate
|
package/{fs.js → node.js}
RENAMED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {readFileSync} from 'node:fs'
|
|
2
2
|
import {join} from "node:path"
|
|
3
|
-
import {
|
|
3
|
+
import {ZzzTemplateBase} from "./index.js"
|
|
4
4
|
|
|
5
5
|
export * from './index.js'
|
|
6
6
|
|
|
7
|
-
export class
|
|
7
|
+
export class ZzzTemplateNode extends ZzzTemplateBase {
|
|
8
8
|
constructor($this = {}) {
|
|
9
9
|
super($this)
|
|
10
10
|
this._dir = $this.dir || ''
|
|
@@ -13,3 +13,6 @@ export class ZzzFs extends ZzzBase {
|
|
|
13
13
|
return readFileSync(join(this._dir, f), "utf8")
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
|
|
17
|
+
// @deprecated
|
|
18
|
+
export const ZzzFs = ZzzTemplateNode
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zzz-template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Fastest JavaScript template engine using native template literals. Zero dependencies, ~500 bytes, works in Node.js and browser. EJS/Handlebars alternative.",
|
|
6
6
|
"author": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"files": [
|
|
44
44
|
"index.js",
|
|
45
45
|
"index.d.ts",
|
|
46
|
-
"
|
|
46
|
+
"node.js",
|
|
47
47
|
"fs.d.ts",
|
|
48
48
|
"readme.md",
|
|
49
49
|
"LICENSE"
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
"import": "./index.js",
|
|
58
58
|
"require": "./index.js"
|
|
59
59
|
},
|
|
60
|
-
"./
|
|
60
|
+
"./node.js": {
|
|
61
61
|
"types": "./fs.d.ts",
|
|
62
|
-
"import": "./
|
|
63
|
-
"require": "./
|
|
62
|
+
"import": "./node.js",
|
|
63
|
+
"require": "./node.js"
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
}
|