miki-template 2.0.1 → 2.2.3

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 (71) hide show
  1. package/.github/workflows/ci.yml +13 -37
  2. package/.github/workflows/docs.yml +105 -0
  3. package/.github/workflows/release.yml +6 -0
  4. package/README.md +69 -14
  5. package/assets/logo.png +0 -0
  6. package/benchmarks/ejs-results.json +4 -4
  7. package/benchmarks/handlebars-results.json +6 -6
  8. package/benchmarks/miki-results.json +4 -4
  9. package/benchmarks/pug-results.json +4 -4
  10. package/benchmarks/stress.mjs +1 -1
  11. package/docs/api/async-render.md +85 -0
  12. package/docs/api/cache.md +87 -0
  13. package/docs/api/compile.md +128 -0
  14. package/docs/api/context-processors.md +77 -0
  15. package/docs/api/filters.md +217 -0
  16. package/docs/api/finder.md +94 -0
  17. package/docs/api/helpers.md +53 -0
  18. package/docs/api/i18n.md +157 -0
  19. package/docs/api/index.md +54 -0
  20. package/docs/api/libraries.md +207 -0
  21. package/docs/api/render-partial.md +81 -0
  22. package/docs/api/render.md +92 -0
  23. package/docs/api/security.md +145 -0
  24. package/docs/api/setup-express.md +76 -0
  25. package/docs/api/tags.md +134 -0
  26. package/docs/assets/banner.png +0 -0
  27. package/docs/assets/logo.png +0 -0
  28. package/docs/guide/advanced-usage.md +397 -0
  29. package/docs/guide/async-rendering.md +308 -0
  30. package/docs/guide/context-processors.md +257 -0
  31. package/docs/guide/custom-filters.md +311 -0
  32. package/docs/guide/custom-tags.md +271 -0
  33. package/docs/guide/filters.md +642 -0
  34. package/docs/guide/getting-started.md +102 -0
  35. package/docs/guide/installation.md +95 -0
  36. package/docs/guide/partial-templates.md +367 -0
  37. package/docs/guide/quick-start.md +222 -0
  38. package/docs/guide/security.md +345 -0
  39. package/docs/guide/tags.md +783 -0
  40. package/docs/guide/template-discovery.md +170 -0
  41. package/docs/guide/template-inheritance.md +273 -0
  42. package/docs/guide/what-is-miki-template.md +28 -0
  43. package/docs/guide/why-miki-template.md +75 -0
  44. package/docs/index.md +104 -0
  45. package/docs/integrations/elysia.md +78 -0
  46. package/docs/integrations/express.md +219 -0
  47. package/docs/integrations/fastify.md +77 -0
  48. package/docs/integrations/hono.md +78 -0
  49. package/docs/integrations/index.md +68 -0
  50. package/docs/integrations/koa.md +88 -0
  51. package/docs/integrations/nestjs.md +78 -0
  52. package/docs/integrations/tsed.md +81 -0
  53. package/docs/javascripts/extra.js +174 -0
  54. package/docs/performance.md +37 -0
  55. package/docs/stylesheets/extra.css +819 -0
  56. package/mkdocs.yml +217 -0
  57. package/overrides/main.html +26 -0
  58. package/overrides/partials/footer.html +9 -0
  59. package/package.json +4 -2
  60. package/requirements-docs.txt +1 -0
  61. package/docs/README.md +0 -18
  62. package/docs/advanced_usage.md +0 -71
  63. package/docs/api.md +0 -122
  64. package/docs/filters.md +0 -708
  65. package/docs/installation.md +0 -106
  66. package/docs/integrations.md +0 -214
  67. package/docs/overview.md +0 -79
  68. package/docs/partialdef.md +0 -70
  69. package/docs/security.md +0 -27
  70. package/docs/tags.md +0 -673
  71. package/docs/usage.md +0 -646
@@ -0,0 +1,128 @@
1
+ # compile()
2
+
3
+ Compile a template string into a reusable renderable object.
4
+
5
+ ## Signature
6
+
7
+ ```javascript
8
+ compile(templateStr, options = {})
9
+ ```
10
+
11
+ ## Parameters
12
+
13
+ | Parameter | Type | Description |
14
+ |-----------|------|-------------|
15
+ | `templateStr` | `string` | Template source string |
16
+ | `options` | `object` | Options including `views` directories |
17
+
18
+ ## Returns
19
+
20
+ An object with these render methods:
21
+
22
+ | Method | Description |
23
+ |--------|-------------|
24
+ | `render(contextObj, callOptions)` | Synchronous render |
25
+ | `renderWith(contextObj, callOptions)` | Render with options override |
26
+ | `asyncRender(contextObj)` | Asynchronous render (supports async filters/tags) |
27
+ | `asyncRenderWith(contextObj, callOptions)` | Async render with options override |
28
+ | `renderBlock(blockName, contextObj)` | Render a single `{% block %}` |
29
+ | `renderPartial(partialName, contextObj)` | Render a named `{% partialdef %}` |
30
+
31
+ ## Examples
32
+
33
+ ### Basic compile
34
+
35
+ === "CommonJS"
36
+
37
+ ```javascript
38
+ const { compile } = require('miki-template');
39
+
40
+ const compiled = compile('<h1>{{ title }}</h1>');
41
+
42
+ const html = compiled.render({ title: 'Hello' });
43
+ // Output: <h1>Hello</h1>
44
+ ```
45
+
46
+ === "ES Modules"
47
+
48
+ ```javascript
49
+ import { compile } from 'miki-template';
50
+
51
+ const compiled = compile('<h1>{{ title }}</h1>');
52
+
53
+ const html = compiled.render({ title: 'Hello' });
54
+ // Output: <h1>Hello</h1>
55
+ ```
56
+
57
+ ### Render with options override
58
+
59
+ === "CommonJS"
60
+
61
+ ```javascript
62
+ const { compile } = require('miki-template');
63
+
64
+ const compiled = compile(template, { views: './templates' });
65
+ const html = compiled.renderWith({ title: 'Hello' }, { views: './other-views' });
66
+ ```
67
+
68
+ === "ES Modules"
69
+
70
+ ```javascript
71
+ import { compile } from 'miki-template';
72
+
73
+ const compiled = compile(template, { views: './templates' });
74
+ const html = compiled.renderWith({ title: 'Hello' }, { views: './other-views' });
75
+ ```
76
+
77
+ ### Render a Block (template inheritance)
78
+
79
+ === "CommonJS"
80
+
81
+ ```javascript
82
+ const { compile } = require('miki-template');
83
+
84
+ const compiled = compile(childTemplate, { views: './templates' });
85
+ const html = compiled.renderBlock('content', context);
86
+ ```
87
+
88
+ === "ES Modules"
89
+
90
+ ```javascript
91
+ import { compile } from 'miki-template';
92
+
93
+ const compiled = compile(childTemplate, { views: './templates' });
94
+ const html = compiled.renderBlock('content', context);
95
+ ```
96
+
97
+ ### Render a Partial
98
+
99
+ === "CommonJS"
100
+
101
+ ```javascript
102
+ const { compile } = require('miki-template');
103
+
104
+ const compiled = compile(`
105
+ {% partialdef card %}
106
+ <div class="card">{{ title }}</div>
107
+ {% endpartialdef %}
108
+ `);
109
+ const html = compiled.renderPartial('card', { title: 'Hello' });
110
+ ```
111
+
112
+ === "ES Modules"
113
+
114
+ ```javascript
115
+ import { compile } from 'miki-template';
116
+
117
+ const compiled = compile(`
118
+ {% partialdef card %}
119
+ <div class="card">{{ title }}</div>
120
+ {% endpartialdef %}
121
+ `);
122
+ const html = compiled.renderPartial('card', { title: 'Hello' });
123
+ ```
124
+
125
+ ## Related
126
+
127
+ - [render()](./render)
128
+ - [asyncRender()](./async-render)
@@ -0,0 +1,77 @@
1
+ # Context Processors API
2
+
3
+ ## registerContextProcessor
4
+
5
+ Register a context processor function that runs before every render. The returned object is merged into the rendering context, with explicit context values always winning.
6
+
7
+ === "CommonJS"
8
+
9
+ ```javascript
10
+ const { registerContextProcessor } = require('miki-template');
11
+
12
+ registerContextProcessor((context) => {
13
+ return {
14
+ siteName: 'My App',
15
+ currentYear: new Date().getFullYear()
16
+ };
17
+ });
18
+ ```
19
+
20
+ === "ES Modules"
21
+
22
+ ```javascript
23
+ import { registerContextProcessor } from 'miki-template';
24
+
25
+ registerContextProcessor((context) => {
26
+ return {
27
+ siteName: 'My App',
28
+ currentYear: new Date().getFullYear()
29
+ };
30
+ });
31
+ ```
32
+
33
+ ### Signature
34
+
35
+ ```typescript
36
+ type ContextProcessor = (context: Context) => Record<string, any> | null
37
+ ```
38
+
39
+ - Receives the `Context` object, allowing inspection of existing values via `context.get('key')`.
40
+ - Must return a plain object. Returning `null` or `undefined` is treated as `{}`.
41
+ - Must be synchronous — no async/await or Promises.
42
+
43
+ ## clearContextProcessors
44
+
45
+ Clear all registered context processors. Useful in tests or when re-configuring.
46
+
47
+ === "CommonJS"
48
+
49
+ ```javascript
50
+ const { clearContextProcessors } = require('miki-template');
51
+
52
+ clearContextProcessors();
53
+ ```
54
+
55
+ === "ES Modules"
56
+
57
+ ```javascript
58
+ import { clearContextProcessors } from 'miki-template';
59
+
60
+ clearContextProcessors();
61
+ ```
62
+
63
+ ## Precedence Rules
64
+
65
+ 1. **Context processors run first** — their key/value pairs are added to the context.
66
+ 2. **Your explicit context is applied last** — explicit values always override processor values.
67
+
68
+ ```javascript
69
+ // Processor sets: { siteName: 'My App', theme: 'dark' }
70
+ // You render with: { theme: 'light' }
71
+ // Result: { siteName: 'My App', theme: 'light' }
72
+ ```
73
+
74
+ ## Next Steps
75
+
76
+ - [Context Processors Guide](../guide/context-processors)
77
+ - [API Reference](../)
@@ -0,0 +1,217 @@
1
+ # Filters API
2
+
3
+ ## registerFilter
4
+
5
+ Register a custom filter callable from templates as `{{ value|filter_name:arg }}`.
6
+
7
+ === "CommonJS"
8
+
9
+ ```javascript
10
+ const { registerFilter } = require('miki-template');
11
+
12
+ registerFilter('reverse', (val) => {
13
+ return String(val).split('').reverse().join('');
14
+ });
15
+ ```
16
+
17
+ === "ES Modules"
18
+
19
+ ```javascript
20
+ import { registerFilter } from 'miki-template';
21
+
22
+ registerFilter('reverse', (val) => {
23
+ return String(val).split('').reverse().join('');
24
+ });
25
+ ```
26
+
27
+ ### Filter Signature
28
+
29
+ Filters receive `(value, argument, context)` and must return a string (or `SafeString`):
30
+
31
+ ```javascript
32
+ registerFilter('greet', (val, greeting, context) => {
33
+ return `${greeting}, ${val}!`;
34
+ });
35
+ ```
36
+
37
+ ### Async Filters
38
+
39
+ Filters that return a Promise are awaited automatically when using `asyncRender()`:
40
+
41
+ === "CommonJS"
42
+
43
+ ```javascript
44
+ const { registerFilter, asyncRender } = require('miki-template');
45
+
46
+ registerFilter('fetch_data', async (url) => {
47
+ const res = await fetch(url);
48
+ return res.text();
49
+ });
50
+
51
+ const html = await asyncRender('{{ endpoint|fetch_data }}', {
52
+ endpoint: 'https://api.example.com/data'
53
+ });
54
+ ```
55
+
56
+ === "ES Modules"
57
+
58
+ ```javascript
59
+ import { registerFilter, asyncRender } from 'miki-template';
60
+
61
+ registerFilter('fetch_data', async (url) => {
62
+ const res = await fetch(url);
63
+ return res.text();
64
+ });
65
+
66
+ const html = await asyncRender('{{ endpoint|fetch_data }}', {
67
+ endpoint: 'https://api.example.com/data'
68
+ });
69
+ ```
70
+
71
+ ## getFilter
72
+
73
+ Retrieve a registered filter by name.
74
+
75
+ === "CommonJS"
76
+
77
+ ```javascript
78
+ const { getFilter } = require('miki-template);
79
+
80
+ const reverseFilter = getFilter('reverse');
81
+ console.log(reverseFilter('hello')); // 'olleh'
82
+ ```
83
+
84
+ === "ES Modules"
85
+
86
+ ```javascript
87
+ import { getFilter } from 'miki-template';
88
+
89
+ const reverseFilter = getFilter('reverse');
90
+ ```
91
+
92
+ ## Built-in Filters
93
+
94
+ ### Text Filters
95
+
96
+ | Filter | Description |
97
+ |--------|-------------|
98
+ | `upper` | Uppercase |
99
+ | `lower` | Lowercase |
100
+ | `title` | Title case |
101
+ | `capfirst` | Capitalize first character |
102
+ | `truncatewords:N` | Truncate to N words |
103
+ | `truncatechars:N` | Truncate to N characters (ellipsis) |
104
+ | `truncatechars_html:N` | Truncate to N chars, preserving HTML |
105
+ | `wordcount` | Count words |
106
+ | `striptags` | Remove HTML tags |
107
+ | `slugify` | Convert to URL-friendly slug |
108
+ | `linebreaks` | Convert newlines to `<br>` and `<p>` |
109
+ | `linebreaksbr` | Convert newlines to `<br>` |
110
+ | `length_is:N` | Return length if equals N, else empty |
111
+
112
+ ### HTML Filters
113
+
114
+ | Filter | Description |
115
+ |--------|-------------|
116
+ | `safe` | Mark as safe (no escaping) |
117
+ | `escape` | Force HTML escaping |
118
+
119
+ ### List Filters
120
+
121
+ | Filter | Description |
122
+ |--------|-------------|
123
+ | `length` | Number of items |
124
+ | `join:sep` | Join items with separator |
125
+ | `slice:"start:end"` | Slice a list/string |
126
+ | `dictsort:"key"` | Sort by key (ascending) |
127
+ | `dictsortreversed:"key"` | Sort by key (descending) |
128
+ | `sort` | Sort items |
129
+ | `unique` | Remove duplicates |
130
+ | `random` | Random item |
131
+ | `reverse` | Reverse order |
132
+ | `split:sep` | Split string into list |
133
+ | `replace:"old,new"` | Replace occurrences |
134
+
135
+ ### Default Filters
136
+
137
+ | Filter | Description |
138
+ |--------|-------------|
139
+ | `default:"fallback"` | Show fallback if value is falsy |
140
+ | `default_if_none:"fallback"` | Show fallback if value is `null`/`undefined` |
141
+ | `firstof:v1 v2 v3` | First non-empty value |
142
+
143
+ ### Date/Time Filters
144
+
145
+ | Filter | Description |
146
+ |--------|-------------|
147
+ | `date:"Y-m-d"` | Django-style date format |
148
+ | `time:"H:i"` | Django-style time format |
149
+ | `date_format:"yyyy-MM-dd"` | Intl-style date format |
150
+ | `strftime:"PPPP"` | Intl-style time format |
151
+ | `timesince` | Time since date ("2 hours ago") |
152
+ | `timeuntil` | Time until date |
153
+ | `ago` | Short time-ago ("2m", "3h") |
154
+ | `until` | Short time-until |
155
+ | `time_diff:other_date` | Difference between two dates |
156
+
157
+ ### Numeric Filters
158
+
159
+ | Filter | Description |
160
+ |--------|-------------|
161
+ | `add:N` | Add N |
162
+ | `sub:N` | Subtract N |
163
+ | `mult:N` | Multiply by N |
164
+ | `divisibleby:N` | Check divisibility |
165
+ | `mod:N` | Modulo |
166
+ | `floatformat:N` | Format float with N decimals |
167
+ | `square` | Square a number |
168
+ | `sqrt` | Square root |
169
+ | `abs` | Absolute value |
170
+ | `round:N` | Round to N decimals |
171
+ | `floor` | Floor |
172
+ | `ceil` | Ceiling |
173
+ | `min:N` | Minimum of value and N |
174
+ | `max:N` | Maximum of value and N |
175
+ | `sum` | Sum of list |
176
+ | `average` | Average of list |
177
+
178
+ ### Currency and Data Formatting
179
+
180
+ | Filter | Description |
181
+ |--------|-------------|
182
+ | `currency:"$"` | Format as currency |
183
+ | `phone_number` | Format phone number |
184
+ | `email` | Format as email link |
185
+ | `url` | Format as URL link |
186
+ | `mask:"*"` | Mask sensitive data |
187
+ | `whatsapp_link:"msg"` | Generate WhatsApp link |
188
+ | `credit_card` | Format credit card number |
189
+ | `ssn` | Format SSN |
190
+ | `ip_address` | Format IP address |
191
+ | `uuid` | Format UUID |
192
+ | `filesizeformat` | Human-readable file size |
193
+ | `yesno:"yes,no,maybe"` | Yes/no based on boolean |
194
+ | `pluralize:"s"` | Add plural suffix if needed |
195
+ | `urlencode` | URL-encode |
196
+ | `escapeuri` | Escape URI component |
197
+ | `stringformat:"%s"` | String format |
198
+ | `cut:"text"` | Remove occurrences |
199
+ | `addslashes` | Add slashes |
200
+ | `removetags:"p,div"` | Remove specified tags |
201
+ | `trans:"key"` | Translate key |
202
+ | `regroup:"attr"` | Regroup list by attribute |
203
+ | `json` | Serialize to JSON |
204
+ | `urlize` | Auto-link URLs in text |
205
+
206
+ ### Encoding Filters
207
+
208
+ | Filter | Description |
209
+ |--------|-------------|
210
+ | `base64_encode` | Base64 encode |
211
+ | `base64_decode` | Base64 decode |
212
+
213
+ ## Next Steps
214
+
215
+ - [Filters Guide](../guide/filters)
216
+ - [Custom Filters](../guide/custom-filters)
217
+ - [API Reference](../)
@@ -0,0 +1,94 @@
1
+ # Finder API
2
+
3
+ ## findTemplateInViews
4
+
5
+ Find a template file by name in the provided views directories.
6
+
7
+ === "CommonJS"
8
+
9
+ ```javascript
10
+ const { findTemplateInViews } = require('miki-template');
11
+
12
+ const found = findTemplateInViews('home', ['./views', './app/templates']);
13
+ console.log(found);
14
+ // Output: /absolute/path/to/home.html
15
+ ```
16
+
17
+ === "ES Modules"
18
+
19
+ ```javascript
20
+ import { findTemplateInViews } from 'miki-template';
21
+
22
+ const found = findTemplateInViews('home', ['./views', './app/templates']);
23
+ console.log(found);
24
+ ```
25
+
26
+ ### Parameters
27
+
28
+ | Parameter | Type | Description |
29
+ |-----------|------|-------------|
30
+ | `templateName` | `string` | Template name to search for (with or without extension) |
31
+ | `viewsDirs` | `string[]` | Array of views directories to search |
32
+
33
+ ### Returns
34
+
35
+ `string | null` — Absolute path to the template file, or `null` if not found.
36
+
37
+ ### Behavior
38
+
39
+ - Searches recursively through subdirectories for bare template names (no `/` in name).
40
+ - Tries `.html` and `.miki` extensions when no extension is provided.
41
+ - Also searches app-style `templates/` directories nested inside the views root.
42
+ - Returns the **first match found**.
43
+
44
+ ## setAppTemplateDirNames
45
+
46
+ Configure which directory names are treated as app-style template directories.
47
+
48
+ === "CommonJS"
49
+
50
+ ```javascript
51
+ const { setAppTemplateDirNames } = require('miki-template');
52
+
53
+ setAppTemplateDirNames(['templates', 'views', 'pages']);
54
+ ```
55
+
56
+ === "ES Modules"
57
+
58
+ ```javascript
59
+ import { setAppTemplateDirNames } from 'miki-template';
60
+
61
+ setAppTemplateDirNames(['templates', 'views', 'pages']);
62
+ ```
63
+
64
+ ### Parameters
65
+
66
+ | Parameter | Type | Description |
67
+ |-----------|------|-------------|
68
+ | `names` | `string \| string[]` | Directory name(s) to recognize |
69
+
70
+ ## getAppTemplateDirNames
71
+
72
+ Get the current app template directory names.
73
+
74
+ === "CommonJS"
75
+
76
+ ```javascript
77
+ const { getAppTemplateDirNames } = require('miki-template');
78
+
79
+ console.log(getAppTemplateDirNames());
80
+ // ['templates']
81
+ ```
82
+
83
+ === "ES Modules"
84
+
85
+ ```javascript
86
+ import { getAppTemplateDirNames } from 'miki-template';
87
+
88
+ console.log(getAppTemplateDirNames());
89
+ ```
90
+
91
+ ## Next Steps
92
+
93
+ - [Guide: Smart Template Discovery](../guide/template-discovery)
94
+ - [API Reference](../)
@@ -0,0 +1,53 @@
1
+ # Helpers API
2
+
3
+ ## registerHelper
4
+
5
+ Register a helper function that can be called from templates.
6
+
7
+ === "CommonJS"
8
+
9
+ ```javascript
10
+ const { registerHelper } = require('miki-template');
11
+
12
+ registerHelper('bold', (inner, context) => `<b>${inner}</b>`);
13
+ ```
14
+
15
+ === "ES Modules"
16
+
17
+ ```javascript
18
+ import { registerHelper } from 'miki-template';
19
+
20
+ registerHelper('bold', (inner, context) => `<b>${inner}</b>`);
21
+ ```
22
+
23
+ ### Helper Signature
24
+
25
+ Helpers receive `(content, context)` where `content` is the rendered inner content of the tag:
26
+
27
+ ```javascript
28
+ registerHelper('panel', (content, context) => {
29
+ return `<div class="panel">${content}</div>`;
30
+ });
31
+ ```
32
+
33
+ Usage in templates:
34
+
35
+ ```html
36
+ {% panel %}
37
+ <h2>{{ title }}</h2>
38
+ <p>{{ description }}</p>
39
+ {% endpanel %}
40
+ ```
41
+
42
+ ## Built-in Helpers
43
+
44
+ miki-template includes built-in helpers for common formatting tasks:
45
+
46
+ - `bold` — Wrap content in `<b>` tags
47
+ - `italic` — Wrap content in `<i>` tags
48
+ - `underline` — Wrap content in `<u>` tags
49
+
50
+ ## Next Steps
51
+
52
+ - [Custom Tags](../guide/custom-tags)
53
+ - [API Reference](../)