miki-template 2.2.3 → 2.3.0

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.
Files changed (65) hide show
  1. package/.github/workflows/docs.yml +3 -1
  2. package/.github/workflows/release.yml +0 -5
  3. package/benchmarks/ejs-results.json +6 -6
  4. package/benchmarks/ejs.js +5 -3
  5. package/benchmarks/handlebars-results.json +6 -6
  6. package/benchmarks/handlebars.js +5 -8
  7. package/benchmarks/miki-results.json +6 -6
  8. package/benchmarks/miki.js +6 -3
  9. package/benchmarks/pug-results.json +6 -6
  10. package/benchmarks/pug.js +5 -3
  11. package/docs/api/async-render.md +88 -3
  12. package/docs/api/cache.md +90 -3
  13. package/docs/api/compile.md +131 -3
  14. package/docs/api/context-processors.md +80 -3
  15. package/docs/api/filters.md +223 -3
  16. package/docs/api/finder.md +97 -3
  17. package/docs/api/helpers.md +56 -3
  18. package/docs/api/i18n.md +160 -3
  19. package/docs/api/index.md +82 -28
  20. package/docs/api/libraries.md +210 -3
  21. package/docs/api/render-partial.md +84 -3
  22. package/docs/api/render.md +95 -3
  23. package/docs/api/security.md +148 -3
  24. package/docs/api/setup-express.md +78 -2
  25. package/docs/api/tags.md +138 -4
  26. package/docs/filter.md +0 -0
  27. package/docs/guide/advanced-usage.md +403 -6
  28. package/docs/guide/async-rendering.md +312 -4
  29. package/docs/guide/context-processors.md +261 -4
  30. package/docs/guide/custom-filters.md +315 -4
  31. package/docs/guide/custom-tags.md +275 -4
  32. package/docs/guide/filters.md +675 -3
  33. package/docs/guide/getting-started.md +109 -7
  34. package/docs/guide/installation.md +99 -4
  35. package/docs/guide/partial-templates.md +371 -4
  36. package/docs/guide/quick-start.md +228 -6
  37. package/docs/guide/security.md +348 -3
  38. package/docs/guide/tags.md +789 -6
  39. package/docs/guide/template-discovery.md +174 -4
  40. package/docs/guide/template-inheritance.md +277 -4
  41. package/docs/index.md +24 -42
  42. package/docs/integrations/elysia.md +4 -2
  43. package/docs/integrations/express.md +219 -219
  44. package/docs/integrations/fastify.md +4 -2
  45. package/docs/integrations/hono.md +4 -2
  46. package/docs/integrations/index.md +68 -68
  47. package/docs/integrations/koa.md +4 -2
  48. package/docs/integrations/nestjs.md +4 -2
  49. package/docs/integrations/tsed.md +4 -2
  50. package/docs/performance.md +45 -8
  51. package/ex.mjs +1 -1
  52. package/mkdocs.yml +0 -22
  53. package/overrides/main.html +1 -1
  54. package/package.json +1 -1
  55. package/requirements-docs.txt +2 -1
  56. package/src/codegen.js +905 -0
  57. package/src/context.js +42 -30
  58. package/src/filters.js +16 -0
  59. package/src/index.js +66 -61
  60. package/src/tags/control.js +15 -12
  61. package/src/utils.js +60 -0
  62. package/tests/filters.test.js +9 -0
  63. package/docs/javascripts/extra.js +0 -174
  64. package/docs/stylesheets/extra.css +0 -819
  65. package/overrides/partials/footer.html +0 -9
@@ -1,207 +1,414 @@
1
- # Libraries API
1
+ # Libraries API
2
+
3
+
2
4
 
3
5
  ## registerLibrary
4
6
 
7
+
8
+
5
9
  Register a library — a named bundle of filters, tags, and helpers.
6
10
 
11
+
12
+
7
13
  === "CommonJS"
8
14
 
15
+
16
+
9
17
  ```javascript
18
+
10
19
  const { registerLibrary } = require('miki-template');
11
20
 
21
+
22
+
12
23
  registerLibrary('mylib', {
24
+
13
25
  filters: {
26
+
14
27
  shout: (val) => String(val).toUpperCase() + '!'
28
+
15
29
  },
30
+
16
31
  tags: {
32
+
17
33
  hello: (tagContent, parser) => ({
34
+
18
35
  render: (context) => 'Hello!'
36
+
19
37
  })
38
+
20
39
  },
40
+
21
41
  helpers: {
42
+
22
43
  bold: (inner, context) => `<b>${inner}</b>`
44
+
23
45
  }
46
+
24
47
  });
48
+
25
49
  ```
26
50
 
51
+
52
+
27
53
  === "ES Modules"
28
54
 
55
+
56
+
29
57
  ```javascript
58
+
30
59
  import { registerLibrary } from 'miki-template';
31
60
 
61
+
62
+
32
63
  registerLibrary('mylib', {
64
+
33
65
  filters: {
66
+
34
67
  shout: (val) => String(val).toUpperCase() + '!'
68
+
35
69
  },
70
+
36
71
  tags: {
72
+
37
73
  hello: (tagContent, parser) => ({
74
+
38
75
  render: (context) => 'Hello!'
76
+
39
77
  })
78
+
40
79
  },
80
+
41
81
  helpers: {
82
+
42
83
  bold: (inner, context) => `<b>${inner}</b>`
84
+
43
85
  }
86
+
44
87
  });
88
+
45
89
  ```
46
90
 
91
+
92
+
47
93
  ## activateLibrary
48
94
 
95
+
96
+
49
97
  Activate a library so its filters and tags become available.
50
98
 
99
+
100
+
51
101
  === "CommonJS"
52
102
 
103
+
104
+
53
105
  ```javascript
106
+
54
107
  const { activateLibrary } = require('miki-template');
55
108
 
109
+
110
+
56
111
  activateLibrary('mylib');
112
+
57
113
  ```
58
114
 
115
+
116
+
59
117
  === "ES Modules"
60
118
 
119
+
120
+
61
121
  ```javascript
122
+
62
123
  import { activateLibrary } from 'miki-template';
63
124
 
125
+
126
+
64
127
  activateLibrary('mylib');
128
+
65
129
  ```
66
130
 
131
+
132
+
67
133
  Built-in libraries (`humanize`, `cache`, `lorem`, `markdown`, `i18n`) are auto-activated on import.
68
134
 
135
+
136
+
69
137
  ## unregisterLibrary
70
138
 
139
+
140
+
71
141
  === "CommonJS"
72
142
 
143
+
144
+
73
145
  ```javascript
146
+
74
147
  const { unregisterLibrary } = require('miki-template');
75
148
 
149
+
150
+
76
151
  // Unregister specific library
152
+
77
153
  unregisterLibrary('mylib');
78
154
 
155
+
156
+
79
157
  // Unregister all libraries
158
+
80
159
  unregisterLibrary();
160
+
81
161
  ```
82
162
 
163
+
164
+
83
165
  === "ES Modules"
84
166
 
167
+
168
+
85
169
  ```javascript
170
+
86
171
  import { unregisterLibrary } from 'miki-template';
87
172
 
173
+
174
+
88
175
  unregisterLibrary('mylib');
176
+
89
177
  ```
90
178
 
179
+
180
+
91
181
  ## hasLibrary
92
182
 
183
+
184
+
93
185
  === "CommonJS"
94
186
 
187
+
188
+
95
189
  ```javascript
190
+
96
191
  const { hasLibrary } = require('miki-template');
97
192
 
193
+
194
+
98
195
  if (hasLibrary('humanize')) {
196
+
99
197
  // library is registered
198
+
100
199
  }
200
+
101
201
  ```
102
202
 
203
+
204
+
103
205
  === "ES Modules"
104
206
 
207
+
208
+
105
209
  ```javascript
210
+
106
211
  import { hasLibrary } from 'miki-template';
107
212
 
213
+
214
+
108
215
  if (hasLibrary('humanize')) {
216
+
109
217
  // library is registered
218
+
110
219
  }
220
+
111
221
  ```
112
222
 
223
+
224
+
113
225
  ## getLibrary
114
226
 
227
+
228
+
115
229
  Retrieve a library by name.
116
230
 
231
+
232
+
117
233
  === "CommonJS"
118
234
 
235
+
236
+
119
237
  ```javascript
238
+
120
239
  const { getLibrary } = require('miki-template');
121
240
 
241
+
242
+
122
243
  const lib = getLibrary('humanize');
244
+
123
245
  console.log(Object.keys(lib.filters));
246
+
124
247
  ```
125
248
 
249
+
250
+
126
251
  === "ES Modules"
127
252
 
253
+
254
+
128
255
  ```javascript
256
+
129
257
  import { getLibrary } from 'miki-template';
130
258
 
259
+
260
+
131
261
  const lib = getLibrary('humanize');
262
+
132
263
  ```
133
264
 
265
+
266
+
134
267
  ## getLibraryNames
135
268
 
269
+
270
+
136
271
  === "CommonJS"
137
272
 
273
+
274
+
138
275
  ```javascript
276
+
139
277
  const { getLibraryNames } = require('miki-template');
140
278
 
279
+
280
+
141
281
  console.log(getLibraryNames());
282
+
142
283
  // ['humanize', 'cache', 'lorem']
284
+
143
285
  ```
144
286
 
287
+
288
+
145
289
  === "ES Modules"
146
290
 
291
+
292
+
147
293
  ```javascript
294
+
148
295
  import { getLibraryNames } from 'miki-template';
149
296
 
297
+
298
+
150
299
  console.log(getLibraryNames());
300
+
151
301
  ```
152
302
 
303
+
304
+
153
305
  ## registerLibraryFromPath
154
306
 
307
+
308
+
155
309
  Load a library from a JavaScript file on disk. The file must export `{ tags, filters, helpers }`.
156
310
 
311
+
312
+
157
313
  === "CommonJS"
158
314
 
315
+
316
+
159
317
  ```javascript
318
+
160
319
  const { registerLibraryFromPath } = require('miki-template');
161
320
 
321
+
322
+
162
323
  registerLibraryFromPath('mylib', './libs/mylib.js');
324
+
163
325
  ```
164
326
 
327
+
328
+
165
329
  === "ES Modules"
166
330
 
331
+
332
+
167
333
  ```javascript
334
+
168
335
  import { registerLibraryFromPath } from 'miki-template';
169
336
 
337
+
338
+
170
339
  await registerLibraryFromPath('mylib', './libs/mylib.mjs');
340
+
171
341
  ```
172
342
 
343
+
344
+
173
345
  ## Built-in Libraries
174
346
 
347
+
348
+
175
349
  ### humanize
176
350
 
351
+
352
+
177
353
  Provides natural formatting filters: `intcomma`, `intword`, `ordinal`, `naturalday`, `naturaltime`.
178
354
 
355
+
356
+
179
357
  | Filter | Description |
358
+
180
359
  |--------|-------------|
360
+
181
361
  | `intcomma` | `1234567` → `1,234,567` |
362
+
182
363
  | `intword` | `1234567` → `1.2M` |
364
+
183
365
  | `ordinal` | `1` → `1st`, `2` → `2nd` |
366
+
184
367
  | `naturalday` | Format dates as "today", "yesterday" |
368
+
185
369
  | `naturaltime` | Format times as "just now", "2 hours ago" |
370
+
186
371
  | `ordinal` | Convert numbers to ordinal (1st, 2nd, 3rd) |
187
372
 
373
+
374
+
188
375
  ### cache
189
376
 
377
+
378
+
190
379
  Provides `{% cache timeout key %}...{% endcache %}` tag for caching template fragments.
191
380
 
381
+
382
+
192
383
  ### lorem
193
384
 
385
+
386
+
194
387
  Provides `{% lorem count random_words %}` tag and `lorem` filter for placeholder text.
195
388
 
389
+
390
+
196
391
  ### markdown
197
392
 
393
+
394
+
198
395
  Provides `markdown` filter for Markdown→HTML conversion.
199
396
 
397
+
398
+
200
399
  ### i18n
201
400
 
401
+
402
+
202
403
  Provides `{% trans %}`, `{% blocktrans %}`, `{% language %}` tags and translation functions.
203
404
 
405
+
406
+
204
407
  ## Next Steps
205
408
 
206
- - [Advanced Usage: Libraries](../guide/advanced-usage#library-system)
207
- - [API Reference](../)
409
+
410
+
411
+ - [Advanced Usage: Libraries](../guide/advanced-usage.md#library-system)
412
+
413
+ - [API Reference](../index.md)
414
+
@@ -1,81 +1,162 @@
1
- # renderPartialFromFile / renderPartialFromSource
1
+ # renderPartialFromFile / renderPartialFromSource
2
+
3
+
2
4
 
3
5
  Render a named partial from a template file or source string.
4
6
 
7
+
8
+
5
9
  ## renderPartialFromFile
6
10
 
11
+
12
+
7
13
  Render a named partial from a template file.
8
14
 
15
+
16
+
9
17
  ```javascript
18
+
10
19
  renderPartialFromFile(fileName, partialName, contextObj, options)
20
+
11
21
  ```
12
22
 
23
+
24
+
13
25
  ### Parameters
14
26
 
27
+
28
+
15
29
  | Parameter | Type | Description |
30
+
16
31
  |-----------|------|-------------|
32
+
17
33
  | `fileName` | `string` | Template file name (without extension) |
34
+
18
35
  | `partialName` | `string` | Name of the partial to render |
36
+
19
37
  | `contextObj` | `object` | Variables to inject |
38
+
20
39
  | `options` | `object` | Options including `views` directories |
21
40
 
41
+
42
+
22
43
  ### Example
23
44
 
45
+
46
+
24
47
  === "CommonJS"
25
48
 
49
+
50
+
26
51
  ```javascript
52
+
27
53
  const { renderPartialFromFile } = require('miki-template');
28
54
 
55
+
56
+
29
57
  const html = renderPartialFromFile('home', 'card', { title: 'Hello' }, { views: './views' });
58
+
30
59
  ```
31
60
 
61
+
62
+
32
63
  === "ES Modules"
33
64
 
65
+
66
+
34
67
  ```javascript
68
+
35
69
  import { renderPartialFromFile } from 'miki-template';
36
70
 
71
+
72
+
37
73
  const html = renderPartialFromFile('home', 'card', { title: 'Hello' }, { views: './views' });
74
+
38
75
  ```
39
76
 
77
+
78
+
40
79
  ## renderPartialFromSource
41
80
 
81
+
82
+
42
83
  Render a named partial from a template source string.
43
84
 
85
+
86
+
44
87
  ```javascript
88
+
45
89
  renderPartialFromSource(fileContent, partialName, contextObj, options, filePath?)
90
+
46
91
  ```
47
92
 
93
+
94
+
48
95
  ### Parameters
49
96
 
97
+
98
+
50
99
  | Parameter | Type | Description |
100
+
51
101
  |-----------|------|-------------|
102
+
52
103
  | `fileContent` | `string` | Template source string |
104
+
53
105
  | `partialName` | `string` | Name of the partial to render |
106
+
54
107
  | `contextObj` | `object` | Variables to inject |
108
+
55
109
  | `options` | `object` | Options |
110
+
56
111
  | `filePath` | `string?` | Optional file path for error messages |
57
112
 
113
+
114
+
58
115
  ### Example
59
116
 
117
+
118
+
60
119
  === "CommonJS"
61
120
 
121
+
122
+
62
123
  ```javascript
124
+
63
125
  const { renderPartialFromSource } = require('miki-template');
64
126
 
127
+
128
+
65
129
  const source = `{% partialdef card %}<div>{{ title }}</div>{% endpartialdef %}`;
130
+
66
131
  const html = renderPartialFromSource(source, 'card', { title: 'Hello' });
132
+
67
133
  ```
68
134
 
135
+
136
+
69
137
  === "ES Modules"
70
138
 
139
+
140
+
71
141
  ```javascript
142
+
72
143
  import { renderPartialFromSource } from 'miki-template';
73
144
 
145
+
146
+
74
147
  const source = `{% partialdef card %}<div>{{ title }}</div>{% endpartialdef %}`;
148
+
75
149
  const html = renderPartialFromSource(source, 'card', { title: 'Hello' });
150
+
76
151
  ```
77
152
 
153
+
154
+
78
155
  ## Related
79
156
 
80
- - [render()](./render)
81
- - [compile()](./compile)
157
+
158
+
159
+ - [render()](./render.md)
160
+
161
+ - [compile()](./compile.md)
162
+