miki-template 1.2.0 → 1.3.6

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 (105) hide show
  1. package/.github/release-notes/v1.3.1.md +55 -0
  2. package/.github/release-notes/v1.3.3.md +77 -0
  3. package/.github/workflows/ci.yml +38 -54
  4. package/.github/workflows/release.yml +106 -0
  5. package/AGENT.md +71 -71
  6. package/API_REFERENCE.md +314 -314
  7. package/CHANGELOG.md +173 -97
  8. package/CODE_OF_CONDUCT.md +14 -14
  9. package/CONTRIBUTING.md +27 -27
  10. package/README.md +342 -304
  11. package/ROADMAP.md +40 -40
  12. package/assets/banner.png +0 -0
  13. package/benchmarks/report.json +16 -16
  14. package/benchmarks/run.js +49 -49
  15. package/benchmarks/stress.mjs +647 -0
  16. package/benchmarks/templates/large.dtpl +7 -7
  17. package/benchmarks/templates/medium.dtpl +3 -3
  18. package/benchmarks/templates/small.dtpl +7 -7
  19. package/context/component.md +109 -109
  20. package/context/prd.md +131 -131
  21. package/context/project-structure.md +33 -33
  22. package/dir/base.html +23 -0
  23. package/dir/cmpnt.html +11 -0
  24. package/dir/footer.html +3 -0
  25. package/dir/home.html +80 -0
  26. package/dir/index.html +80 -0
  27. package/dir/navbar.html +9 -0
  28. package/docs/README.md +18 -18
  29. package/docs/advanced_usage.md +71 -71
  30. package/docs/api.md +119 -102
  31. package/docs/filters.md +708 -540
  32. package/docs/installation.md +106 -106
  33. package/docs/overview.md +57 -57
  34. package/docs/partialdef.md +70 -41
  35. package/docs/security.md +27 -27
  36. package/docs/tags.md +673 -610
  37. package/docs/usage.md +646 -599
  38. package/eslint.config.mjs +42 -34
  39. package/ex.mjs +33 -0
  40. package/miki-template-extension/.github/workflows/ci.yml +116 -0
  41. package/miki-template-extension/.vscodeignore +7 -0
  42. package/miki-template-extension/CHANGELOG.md +99 -0
  43. package/miki-template-extension/LICENSE +21 -21
  44. package/miki-template-extension/README.md +273 -82
  45. package/miki-template-extension/extension.js +1013 -0
  46. package/miki-template-extension/icon.png +0 -0
  47. package/miki-template-extension/icon.svg +10 -10
  48. package/miki-template-extension/miki-template-1.7.1.vsix +0 -0
  49. package/miki-template-extension/package.json +280 -46
  50. package/miki-template-extension/snippets/miki-template.json +717 -177
  51. package/miki-template-extension/syntaxes/language-configuration.json +114 -26
  52. package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +355 -146
  53. package/miki-template-extension/tests/grammar-tests.json +162 -0
  54. package/miki-template-extension/tests/run-grammar-tests.js +82 -0
  55. package/package.json +37 -31
  56. package/sample-app/package-lock.json +901 -0
  57. package/sample-app/package.json +9 -0
  58. package/sample-app/server.js +14 -0
  59. package/sample-app/views/index.html +1 -0
  60. package/scripts/build-vsix.js +129 -0
  61. package/scripts/build-vsix.ps1 +15 -0
  62. package/snippets/miki-template.json +177 -177
  63. package/src/asyncRender.js +20 -20
  64. package/src/cache.js +80 -41
  65. package/src/context.js +126 -122
  66. package/src/context_processors.js +48 -41
  67. package/src/esm.mjs +84 -72
  68. package/src/filters.js +975 -527
  69. package/src/i18n.js +171 -171
  70. package/src/index.js +974 -454
  71. package/src/lexer.js +114 -92
  72. package/src/libraries.js +371 -240
  73. package/src/parser.js +270 -250
  74. package/src/security.js +53 -51
  75. package/src/tags/control.js +719 -590
  76. package/src/tags/extra.js +154 -0
  77. package/src/tags/helpers.js +26 -26
  78. package/src/tags/i18n.js +256 -230
  79. package/src/tags/inheritance.js +335 -216
  80. package/src/tags/registry.js +18 -18
  81. package/src/tags/util.js +400 -322
  82. package/src/types.d.ts +107 -107
  83. package/syntaxes/language-configuration.json +26 -26
  84. package/syntaxes/miki-template.tmLanguage.json +146 -146
  85. package/tests/asyncRender.test.js +17 -17
  86. package/tests/base.html +6 -6
  87. package/tests/child.html +3 -3
  88. package/tests/context_processors.test.js +13 -13
  89. package/tests/esm.test.mjs +61 -26
  90. package/tests/filters.test.js +254 -99
  91. package/tests/include_security.test.js +9 -9
  92. package/tests/integration/README.md +32 -0
  93. package/tests/integration/features.test.cjs +1681 -0
  94. package/tests/integration/features.test.mjs +1697 -0
  95. package/tests/integration/templates/base.miki +6 -0
  96. package/tests/integration/templates/child.miki +6 -0
  97. package/tests/integration/templates/index.html +17 -0
  98. package/tests/lexer.test.js +45 -45
  99. package/tests/parser.test.js +57 -55
  100. package/tests/partial.html +1 -1
  101. package/tests/partialdef.test.js +79 -40
  102. package/tests/production_checks.js +57 -57
  103. package/tests/security.test.js +28 -28
  104. package/tests/tags.test.js +233 -203
  105. package/miki-template-1.2.0.vsix +0 -0
package/dir/index.html ADDED
@@ -0,0 +1,80 @@
1
+ {% extends 'base.html' %}
2
+
3
+ {% block title %}
4
+ {{ siteName|default:'Home page Here'|title }} | {{ block.super }}
5
+ {% endblock %}
6
+
7
+ {% block content %}
8
+ {% partialdef card inline %}
9
+ <div class="card">
10
+ <h3>{{ title|default:'No title' }}</h3>
11
+ <p>{{ description|default:'{% lorem 20 %}'|upper }}</p>
12
+ </div>
13
+ {% endpartialdef %}
14
+
15
+ {% partial card with title='Hello' description='World' %}
16
+
17
+ {% with a=5 %}
18
+ <marquee behavior="" direction="">
19
+ <span>some new stuff with with tag : {{ a|add:5 }}</span>
20
+ </marquee>
21
+ {% endwith %}
22
+
23
+ {{ name }}
24
+ {{ login.name }}
25
+ {{ login.email }}
26
+ <h1>Users table goes below</h1>
27
+ <table border="3">
28
+ <th>name</th>
29
+ <th>email</th>
30
+ <th>action</th>
31
+
32
+ {% for user in users %}
33
+ <tr>
34
+ <td>{{ user.name|title }}</td>
35
+ <td>{{ user.email }}</td>
36
+ <td>
37
+ <a href="">Delete user</a>
38
+ </td>
39
+ </tr>
40
+ {% endfor %}
41
+
42
+ </table>
43
+ <h1>Data list</h1>
44
+ <h2>data list of data</h2>
45
+ {% for obj in data %}
46
+ {% partialdef mike inline %}
47
+
48
+ <li>{{ obj.name }}</li>
49
+ <li>{{ forloop.counter }}</li>
50
+ <li> {{ obj.email }}</li>
51
+ {% endpartialdef %}
52
+ {% endfor %}
53
+
54
+ <h2>users list of data</h2>
55
+ <ul style="background-color: aqua;color: blueviolet; list-style-type:upper-roman;">
56
+
57
+ {% for obj in users %}
58
+ {% partial mike %}
59
+ {% endfor %}
60
+ </ul>
61
+
62
+ <h3>form with crsf</h3>
63
+
64
+ <form action="">
65
+ {% csrf_token %}
66
+ <input type="text" name="" id="" title="some text"/>
67
+ </form>
68
+ <h3>included content here</h3>
69
+ {% include 'cmpnt.html#me' %}
70
+
71
+ {{"2324"|filesizeformat}}
72
+ {{"224"|filesizeformat}}
73
+ {{ "123456789"|filesizeformat }}
74
+ {{ "1234567890"|filesizeformat }}
75
+ {{ "1234567890123"|filesizeformat }}
76
+ {{ "1234567890123456"|filesizeformat }}
77
+ {{ "1234567890123456789"|filesizeformat }}
78
+
79
+
80
+ {% endblock %}
@@ -0,0 +1,9 @@
1
+ <header>
2
+ <div class="brand">
3
+ <img src="/path/to/logo.png" alt="Logo" width="40px">
4
+ </div>
5
+ <nav>
6
+ <a href="/">Home</a>
7
+
8
+ </nav>
9
+ </header>
package/docs/README.md CHANGED
@@ -1,18 +1,18 @@
1
- # Documentation Index
2
-
3
- Welcome to the **miki-template** documentation hub. Below is a quick navigation guide to all docs.
4
-
5
- | Document | Description |
6
- |---|---|
7
- | [overview.md](overview.md) | High‑level project overview and repository layout |
8
- | [installation.md](installation.md) | How to install the package and prerequisites |
9
- | [usage.md](usage.md) | Basic API usage, rendering, compilation, and block rendering |
10
- | [tags.md](tags.md) | Reference for every built‑in tag (`if`, `for`, `partialdef`, etc.) |
11
- | [filters.md](filters.md) | Comprehensive list of built‑in filters with examples |
12
- | [partialdef.md](partialdef.md) | Detailed guide for the `partialdef` feature (including inline, nesting, API) |
13
- | [security.md](security.md) | Security considerations (auto‑escaping, CSP, CSRF) |
14
- | [api.md](api.md) | Low‑level API surface – `compile`, `renderPartial`, `registerTag`, etc. |
15
- | [contributing.md](contributing.md) | Guidelines for contributing, pull‑request flow, and code style |
16
- | [roadmap.md](roadmap.md) | Future milestones and release schedule |
17
-
18
- All files are located under `c:/Users/Coder Miki/Desktop/miki-template/docs/`.
1
+ # Documentation Index
2
+
3
+ Welcome to the **miki-template** documentation hub. Below is a quick navigation guide to all docs.
4
+
5
+ | Document | Description |
6
+ |---|---|
7
+ | [overview.md](overview.md) | High‑level project overview and repository layout |
8
+ | [installation.md](installation.md) | How to install the package and prerequisites |
9
+ | [usage.md](usage.md) | Basic API usage, rendering, compilation, and block rendering |
10
+ | [tags.md](tags.md) | Reference for every built‑in tag (`if`, `for`, `partialdef`, etc.) |
11
+ | [filters.md](filters.md) | Comprehensive list of built‑in filters with examples |
12
+ | [partialdef.md](partialdef.md) | Detailed guide for the `partialdef` feature (including inline, nesting, API) |
13
+ | [security.md](security.md) | Security considerations (auto‑escaping, CSP, CSRF) |
14
+ | [api.md](api.md) | Low‑level API surface – `compile`, `renderPartial`, `registerTag`, etc. |
15
+ | [contributing.md](contributing.md) | Guidelines for contributing, pull‑request flow, and code style |
16
+ | [roadmap.md](roadmap.md) | Future milestones and release schedule |
17
+
18
+ All files are located under `c:/Users/Coder Miki/Desktop/miki-template/docs/`.
@@ -1,71 +1,71 @@
1
- # Advanced Usage
2
-
3
- This document showcases real‑world examples of the **miki‑template** engine.
4
-
5
- ## 1. Partial Definition & Inclusion
6
- ```django
7
- {% partialdef "header" %}
8
- <header>
9
- <h1>{{ site.title }}</h1>
10
- </header>
11
- {% endpartialdef %}
12
-
13
- {% include "header" %}
14
- ```
15
- *Result:* Renders a reusable header component.
16
-
17
- ---
18
- ## 2. Custom Tag Helper – Markdown
19
- ```js
20
- const { registerHelper } = require('./src/tags/helpers');
21
- const markdownIt = require('markdown-it')();
22
- registerHelper('markdown', (content) => markdownIt.render(content));
23
- ```
24
- ```django
25
- {% markdown %}
26
- # Hello World
27
- * Item 1
28
- * Item 2
29
- {% endmarkdown %}
30
- ```
31
- *Result:* Converts markdown to HTML on‑the‑fly.
32
-
33
- ---
34
- ## 3. Async Rendering with a Sleep Helper
35
- ```js
36
- registerHelper('sleep', async (content) => {
37
- await new Promise(r => setTimeout(r, 50));
38
- return `Awake after ${content}`;
39
- });
40
- ```
41
- ```django
42
- {% sleep %}50ms{% endsleep %}
43
- ```
44
- ```js
45
- const { asyncRender } = require('./src');
46
- asyncRender(template, {} ).then(console.log);
47
- ```
48
- *Result:* `Awake after 50ms`
49
-
50
- ---
51
- ## 4. Extended Date Formatting
52
- ```django
53
- {{ now|date_format:"yyyy-MM-dd HH:mm:ss" }}
54
- ```
55
- *Result:* `2026-08-29 13:48:07`
56
- ### Additional Date Filter Examples
57
-
58
- ```django
59
- {{ now|strftime:"PPpp" }} {# Full date-fns pattern #}
60
- {{ now|date:"Y-m-d H:i:s" }} {# Django style with seconds #}
61
- {{ now|time:"H:i:s" }} {# Time only #}
62
- ```
63
-
64
- *Result:* Example outputs will appear when rendered.
65
-
66
- ---
67
- ## 5. Performance Benchmark (see `benchmarks/run.js`)
68
- The benchmark renders three template sizes (small, medium, large) synchronously and asynchronously, reporting average render time and cache hit ratio.
69
-
70
- ---
71
- > **Tip:** All examples work with the default Express integration via `app.engine('dtpl', require('miki-template').__express);`
1
+ # Advanced Usage
2
+
3
+ This document showcases real‑world examples of the **miki‑template** engine.
4
+
5
+ ## 1. Partial Definition & Inclusion
6
+ ```django
7
+ {% partialdef "header" %}
8
+ <header>
9
+ <h1>{{ site.title }}</h1>
10
+ </header>
11
+ {% endpartialdef %}
12
+
13
+ {% include "header" %}
14
+ ```
15
+ *Result:* Renders a reusable header component.
16
+
17
+ ---
18
+ ## 2. Custom Tag Helper – Markdown
19
+ ```js
20
+ const { registerHelper } = require('./src/tags/helpers');
21
+ const markdownIt = require('markdown-it')();
22
+ registerHelper('markdown', (content) => markdownIt.render(content));
23
+ ```
24
+ ```django
25
+ {% markdown %}
26
+ # Hello World
27
+ * Item 1
28
+ * Item 2
29
+ {% endmarkdown %}
30
+ ```
31
+ *Result:* Converts markdown to HTML on‑the‑fly.
32
+
33
+ ---
34
+ ## 3. Async Rendering with a Sleep Helper
35
+ ```js
36
+ registerHelper('sleep', async (content) => {
37
+ await new Promise(r => setTimeout(r, 50));
38
+ return `Awake after ${content}`;
39
+ });
40
+ ```
41
+ ```django
42
+ {% sleep %}50ms{% endsleep %}
43
+ ```
44
+ ```js
45
+ const { asyncRender } = require('./src');
46
+ asyncRender(template, {} ).then(console.log);
47
+ ```
48
+ *Result:* `Awake after 50ms`
49
+
50
+ ---
51
+ ## 4. Extended Date Formatting
52
+ ```django
53
+ {{ now|date_format:"yyyy-MM-dd HH:mm:ss" }}
54
+ ```
55
+ *Result:* `2026-08-29 13:48:07`
56
+ ### Additional Date Filter Examples
57
+
58
+ ```django
59
+ {{ now|strftime:"PPpp" }} {# Full date-fns pattern #}
60
+ {{ now|date:"Y-m-d H:i:s" }} {# Django style with seconds #}
61
+ {{ now|time:"H:i:s" }} {# Time only #}
62
+ ```
63
+
64
+ *Result:* Example outputs will appear when rendered.
65
+
66
+ ---
67
+ ## 5. Performance Benchmark (see `benchmarks/run.js`)
68
+ The benchmark renders three template sizes (small, medium, large) synchronously and asynchronously, reporting average render time and cache hit ratio.
69
+
70
+ ---
71
+ > **Tip:** All examples work with the default Express integration via `app.engine('dtpl', require('miki-template').__express);`
package/docs/api.md CHANGED
@@ -1,102 +1,119 @@
1
- # API Reference
2
-
3
- This document lists the public API exported by **miki-template** for developers to integrate the engine into their projects.
4
-
5
- | Function / Export | Signature | Description | Example |
6
- |---|---|---|---|
7
- | `compile(templateStr, options?)` | `compile(string, object?) → { render, asyncRender, renderBlock, renderPartial }` | Compiles a template string into a renderable object. Optional `options` can include `views` directories, custom tags/filters, etc. | `const tpl = compile('Hello {{ name }}');` |
8
- | `render(templateStr, context?, options?)` | `render(string, object?, object?) → string` | One‑off rendering of a template string with the provided context. | `render('Hello {{ name }}', { name: 'World' });` |
9
- | `asyncRender(templateStr, context?, options?)` | `asyncRender(string, object?, object?) → Promise<string>` | Asynchronous rendering (useful with async helpers). | `await asyncRender(tpl, ctx);` |
10
- | `__express(filePath, options, callback)` | `__express(string, object, function)` | Express view engine adapter – reads the file at `filePath` and renders it. | `app.set('view engine', 'miki');` |
11
- | `__expressAsync(filePath, options)` | `__expressAsync(string, object) → Promise<string>` | Async Express 5+ view engine adapter. Returns a Promise that resolves to rendered HTML. | `app.engine('html', miki.__expressAsync);` |
12
- | `stripExpressContext(options)` | `stripExpressContext(object) → object` | Remove Express framework keys (`_locals`, `settings`, `cache`) from an options object. | `const ctx = stripExpressContext(res.locals);` |
13
- | `clearCache()` | `clearCache() → void` | Clear the in-memory compiled template cache. | `clearCache();` |
14
- | `registerTag(name, parserFn)` | `registerTag(string, function)` | Register a custom tag parser. Must be called before compiling templates. | `registerTag('mytag', parserFn);` |
15
- | `registerFilter(name, fn)` | `registerFilter(string, function)` | Register a custom filter. | `registerFilter('reverse', str => str.split('').reverse().join(''));` |
16
- | `getFilter(name)` | `getFilter(string) → function` | Retrieve a registered filter function by name. | `const upper = getFilter('upper');` |
17
- | `registerHelper(name, fn)` | `registerHelper(string, function)` | Register a helper function available inside templates. | `registerHelper('upper', s => s.toUpperCase());` |
18
- | `registerContextProcessor(fn)` | `registerContextProcessor(function)` | Add a context processor that mutates the rendering context before each render. | `registerContextProcessor(ctx => ({ ...ctx, csrf_token: '123' }));` |
19
- | `registerTranslation(lang, messages)` | `registerTranslation(string, object) → void` | Register translation messages for a language. | `registerTranslation('fr', { 'Hello': 'Bonjour' });` |
20
- | `setLanguage(lang)` | `setLanguage(string) → void` | Set the active language for all subsequent renders. | `setLanguage('fr');` |
21
- | `getLanguage()` | `getLanguage() → string` | Get the currently active language. | `const lang = getLanguage();` |
22
- | `setFallbackLanguage(lang)` | `setFallbackLanguage(string) → void` | Set the fallback language for missing translations. | `setFallbackLanguage('en');` |
23
- | `getAvailableLanguages()` | `getAvailableLanguages() → string[]` | List all registered language codes. | `const langs = getAvailableLanguages();` |
24
- | `registerLibrary(name, def)` | `registerLibrary(string, object) → void` | Register a plugin library of tags, filters, and helpers. | `registerLibrary('humanize', { filters: { intcomma } });` |
25
- | `registerLibraryFromPath(name, path)` | `registerLibraryFromPath(string, string) → object` | Load a library from a JS file on disk. | `registerLibraryFromPath('myLib', './libs/my-lib.js');` |
26
- | `activateLibrary(name)` | `activateLibrary(string) → void` | Activate a registered library (makes its tags/filters available). | `activateLibrary('humanize');` |
27
- | `SafeString` | `class SafeString` | Wrapper class for values that should bypass auto‑escaping. Returned by `markSafe`. |
28
- | `markSafe(value)` | `markSafe(any) → SafeString` | Marks a value as safe, preventing HTML escaping. |
29
- | `isSafe(value)` | `isSafe(any) → boolean` | Checks if a value is a `SafeString`. |
30
- | `escapeHtml(str)` | `escapeHtml(string) → string` | Escapes HTML special characters. Used internally for auto‑escaping. |
31
-
32
- All of the above are exported from `src/index.js` and can be imported via:
33
-
34
- ### CommonJS
35
- ```js
36
- const {
37
- compile,
38
- render,
39
- asyncRender,
40
- __express,
41
- __expressAsync,
42
- stripExpressContext,
43
- clearCache,
44
- registerTag,
45
- registerFilter,
46
- getFilter,
47
- registerHelper,
48
- registerContextProcessor,
49
- registerTranslation,
50
- setLanguage,
51
- getLanguage,
52
- setFallbackLanguage,
53
- getAvailableLanguages,
54
- registerLibrary,
55
- registerLibraryFromPath,
56
- activateLibrary,
57
- SafeString,
58
- markSafe,
59
- isSafe,
60
- escapeHtml
61
- } = require('miki-template');
62
- ```
63
-
64
- ### ES Modules (ESM)
65
- ```js
66
- import {
67
- compile,
68
- render,
69
- asyncRender,
70
- __express,
71
- __expressAsync,
72
- stripExpressContext,
73
- clearCache,
74
- registerTag,
75
- registerFilter,
76
- getFilter,
77
- registerHelper,
78
- registerContextProcessor,
79
- registerTranslation,
80
- setLanguage,
81
- getLanguage,
82
- setFallbackLanguage,
83
- getAvailableLanguages,
84
- registerLibrary,
85
- registerLibraryFromPath,
86
- activateLibrary,
87
- SafeString,
88
- markSafe,
89
- isSafe,
90
- escapeHtml
91
- } from 'miki-template';
92
-
93
- // Or import all as default
94
- import miki from 'miki-template';
95
- const { render: mikiRender } = miki;
96
- ```
97
-
98
- For detailed usage, refer to the corresponding sections in the documentation:
99
- - **Usage** – `docs/usage.md`
100
- - **Tags** – `docs/tags.md`
101
- - **Filters** – `docs/filters.md`
102
- - **Security** – `docs/security.md`
1
+ # API Reference
2
+
3
+ This document lists the public API exported by **miki-template** for developers to integrate the engine into their projects.
4
+
5
+ | Function / Export | Signature | Description | Example |
6
+ |---|---|---|---|
7
+ | `compile(templateStr, options?)` | `compile(string, object?) → { render, asyncRender, renderBlock, renderPartial }` | Compiles a template string into a renderable object. Optional `options` can include `views` directories, custom tags/filters, etc. | `const tpl = compile('Hello {{ name }}');` |
8
+ | `render(templateStr, context?, options?)` | `render(string, object?, object?) → string` | One‑off rendering of a template string with the provided context. | `render('Hello {{ name }}', { name: 'World' });` |
9
+ | `asyncRender(templateStr, context?, options?)` | `asyncRender(string, object?, object?) → Promise<string>` | Asynchronous rendering (useful with async helpers). | `await asyncRender(tpl, ctx);` |
10
+ | `__express(filePath, options, callback)` | `__express(string, object, function)` | Express view engine adapter – reads the file at `filePath` and renders it. Honors `view#partial` suffixes for HTMX-style partial responses. | `app.engine('html', miki.__express);` |
11
+ | `__expressAsync(filePath, options)` | `__expressAsync(string, object) → Promise<string>` | Async Express 5+ view engine adapter. Returns a Promise that resolves to rendered HTML. Also honors `view#partial` suffixes. | `app.engine('html', miki.__expressAsync);` |
12
+ | `express(options?)` | `express(object?) → function` | Factory that returns a view-engine function suitable for `app.engine(...)`. Honors `view#partial` selectors. | `app.engine('html', miki.express());` |
13
+ | `setupExpress(app, opts?)` | `setupExpress(expressApp, object?) → void` | **One-line Express integration.** Wires `app.engine(...)`, `app.set('views')`, and patches `res.render` so `res.render('view#partial', ...)` returns just that partial. Options: `{ extension?, views?, async? }`. | `miki.setupExpress(app, { extension: 'html', views: './views' });` |
14
+ | `expressPartialRenderer()` | `expressPartialRenderer() → function` | Express middleware that adds `res.renderPartial(view, locals)`. Useful as a drop-in HTMX helper without the full `setupExpress` shim. | `app.use(miki.expressPartialRenderer());` |
15
+ | `renderPartialFromFile(filePath, partialName, context?, options?)` | `renderPartialFromFile(string, string, object?, object?) → string` | Load a file from disk and render only the named `{% partialdef %}`. | `miki.renderPartialFromFile('views/home.html', 'card', { user });` |
16
+ | `renderPartialFromSource(source, partialName, context?, options?)` | `renderPartialFromSource(string, string, object?, object?) → string` | Render a single named partial directly from a template string. Walks the AST (and `extends` chain) to discover partials nested inside blocks. | `miki.renderPartialFromSource(src, 'card', ctx, { views });` |
17
+ | `stripExpressContext(options)` | `stripExpressContext(object) → object` | Remove Express framework keys (`_locals`, `settings`, `cache`) from an options object. | `const ctx = stripExpressContext(res.locals);` |
18
+ | `clearCache()` | `clearCache() → void` | Clear the in-memory compiled template cache. | `clearCache();` |
19
+ | `registerTag(name, parserFn)` | `registerTag(string, function)` | Register a custom tag parser. Must be called before compiling templates. | `registerTag('mytag', parserFn);` |
20
+ | `registerFilter(name, fn)` | `registerFilter(string, function)` | Register a custom filter. | `registerFilter('reverse', str => str.split('').reverse().join(''));` |
21
+ | `getFilter(name)` | `getFilter(string) → function` | Retrieve a registered filter function by name. | `const upper = getFilter('upper');` |
22
+ | `registerHelper(name, fn)` | `registerHelper(string, function)` | Register a helper function available inside templates. | `registerHelper('upper', s => s.toUpperCase());` |
23
+ | `registerContextProcessor(fn)` | `registerContextProcessor(function)` | Add a context processor that mutates the rendering context before each render. | `registerContextProcessor(ctx => ({ ...ctx, csrf_token: '123' }));` |
24
+ | `registerTranslation(lang, messages)` | `registerTranslation(string, object) → void` | Register translation messages for a language. | `registerTranslation('fr', { 'Hello': 'Bonjour' });` |
25
+ | `setLanguage(lang)` | `setLanguage(string) → void` | Set the active language for all subsequent renders. | `setLanguage('fr');` |
26
+ | `getLanguage()` | `getLanguage() → string` | Get the currently active language. | `const lang = getLanguage();` |
27
+ | `setFallbackLanguage(lang)` | `setFallbackLanguage(string) → void` | Set the fallback language for missing translations. | `setFallbackLanguage('en');` |
28
+ | `getAvailableLanguages()` | `getAvailableLanguages() → string[]` | List all registered language codes. | `const langs = getAvailableLanguages();` |
29
+ | `registerLibrary(name, def)` | `registerLibrary(string, object) → void` | Register a plugin library of tags, filters, and helpers. | `registerLibrary('humanize', { filters: { intcomma } });` |
30
+ | `registerLibraryFromPath(name, path)` | `registerLibraryFromPath(string, string) → object` | Load a library from a JS file on disk. | `registerLibraryFromPath('myLib', './libs/my-lib.js');` |
31
+ | `activateLibrary(name)` | `activateLibrary(string) → void` | Activate a registered library (makes its tags/filters available). | `activateLibrary('humanize');` |
32
+ | `SafeString` | `class SafeString` | Wrapper class for values that should bypass auto‑escaping. Returned by `markSafe`. |
33
+ | `markSafe(value)` | `markSafe(any) → SafeString` | Marks a value as safe, preventing HTML escaping. |
34
+ | `isSafe(value)` | `isSafe(any) → boolean` | Checks if a value is a `SafeString`. |
35
+ | `escapeHtml(str)` | `escapeHtml(string) → string` | Escapes HTML special characters. Used internally for auto‑escaping. |
36
+
37
+ All of the above are exported from `src/index.js` and can be imported via:
38
+
39
+ ### CommonJS
40
+ ```js
41
+ const {
42
+ compile,
43
+ render,
44
+ asyncRender,
45
+ __express,
46
+ __expressAsync,
47
+ express,
48
+ setupExpress,
49
+ expressPartialRenderer,
50
+ renderPartialFromFile,
51
+ renderPartialFromSource,
52
+ stripExpressContext,
53
+ clearCache,
54
+ registerTag,
55
+ registerFilter,
56
+ getFilter,
57
+ registerHelper,
58
+ registerContextProcessor,
59
+ clearContextProcessors,
60
+ registerTranslation,
61
+ setLanguage,
62
+ getLanguage,
63
+ setFallbackLanguage,
64
+ getAvailableLanguages,
65
+ registerLibrary,
66
+ registerLibraryFromPath,
67
+ activateLibrary,
68
+ SafeString,
69
+ markSafe,
70
+ isSafe,
71
+ escapeHtml
72
+ } = require('miki-template');
73
+ ```
74
+
75
+ ### ES Modules (ESM)
76
+ ```js
77
+ import {
78
+ compile,
79
+ render,
80
+ asyncRender,
81
+ __express,
82
+ __expressAsync,
83
+ express,
84
+ setupExpress,
85
+ expressPartialRenderer,
86
+ renderPartialFromFile,
87
+ renderPartialFromSource,
88
+ stripExpressContext,
89
+ clearCache,
90
+ registerTag,
91
+ registerFilter,
92
+ getFilter,
93
+ registerHelper,
94
+ registerContextProcessor,
95
+ clearContextProcessors,
96
+ registerTranslation,
97
+ setLanguage,
98
+ getLanguage,
99
+ setFallbackLanguage,
100
+ getAvailableLanguages,
101
+ registerLibrary,
102
+ registerLibraryFromPath,
103
+ activateLibrary,
104
+ SafeString,
105
+ markSafe,
106
+ isSafe,
107
+ escapeHtml
108
+ } from 'miki-template';
109
+
110
+ // Or import all as default
111
+ import miki from 'miki-template';
112
+ const { render: mikiRender, setupExpress } = miki;
113
+ ```
114
+
115
+ For detailed usage, refer to the corresponding sections in the documentation:
116
+ - **Usage** – `docs/usage.md`
117
+ - **Tags** – `docs/tags.md`
118
+ - **Filters** – `docs/filters.md`
119
+ - **Security** – `docs/security.md`