minijinja-js 2.12.0 → 2.13.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.
@@ -2,31 +2,33 @@
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
4
  export const __wbg_environment_free: (a: number, b: number) => void;
5
- export const environment_new: () => number;
6
- export const environment_addTemplate: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
7
- export const environment_removeTemplate: (a: number, b: number, c: number) => void;
8
- export const environment_clearTemplates: (a: number) => void;
9
- export const environment_renderTemplate: (a: number, b: number, c: number, d: number, e: number) => void;
10
- export const environment_renderStr: (a: number, b: number, c: number, d: number, e: number) => void;
11
- export const environment_renderNamedStr: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
12
- export const environment_evalExpr: (a: number, b: number, c: number, d: number, e: number) => void;
13
5
  export const environment_addFilter: (a: number, b: number, c: number, d: number) => void;
6
+ export const environment_addGlobal: (a: number, b: number, c: number, d: number, e: number) => void;
7
+ export const environment_addTemplate: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
14
8
  export const environment_addTest: (a: number, b: number, c: number, d: number) => void;
15
- export const environment_enablePyCompat: (a: number) => void;
9
+ export const environment_clearTemplates: (a: number) => void;
16
10
  export const environment_debug: (a: number) => number;
17
- export const environment_set_debug: (a: number, b: number) => void;
18
- export const environment_trimBlocks: (a: number) => number;
19
- export const environment_set_trimBlocks: (a: number, b: number) => void;
20
- export const environment_lstripBlocks: (a: number) => number;
21
- export const environment_set_lstripBlocks: (a: number, b: number) => void;
11
+ export const environment_enablePyCompat: (a: number) => void;
12
+ export const environment_evalExpr: (a: number, b: number, c: number, d: number, e: number) => void;
13
+ export const environment_fuel: (a: number) => number;
22
14
  export const environment_keepTrailingNewline: (a: number) => number;
15
+ export const environment_lstripBlocks: (a: number) => number;
16
+ export const environment_new: () => number;
17
+ export const environment_removeGlobal: (a: number, b: number, c: number) => void;
18
+ export const environment_removeTemplate: (a: number, b: number, c: number) => void;
19
+ export const environment_renderNamedStr: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
20
+ export const environment_renderStr: (a: number, b: number, c: number, d: number, e: number) => void;
21
+ export const environment_renderTemplate: (a: number, b: number, c: number, d: number, e: number) => void;
22
+ export const environment_setLoader: (a: number, b: number) => void;
23
+ export const environment_setPathJoinCallback: (a: number, b: number) => void;
24
+ export const environment_set_debug: (a: number, b: number) => void;
25
+ export const environment_set_fuel: (a: number, b: number) => void;
23
26
  export const environment_set_keepTrailingNewline: (a: number, b: number) => void;
24
- export const environment_undefinedBehavior: (a: number) => number;
27
+ export const environment_set_lstripBlocks: (a: number, b: number) => void;
28
+ export const environment_set_trimBlocks: (a: number, b: number) => void;
25
29
  export const environment_set_undefinedBehavior: (a: number, b: number, c: number) => void;
26
- export const environment_fuel: (a: number) => number;
27
- export const environment_set_fuel: (a: number, b: number) => void;
28
- export const environment_addGlobal: (a: number, b: number, c: number, d: number, e: number) => void;
29
- export const environment_removeGlobal: (a: number, b: number, c: number) => void;
30
+ export const environment_trimBlocks: (a: number) => number;
31
+ export const environment_undefinedBehavior: (a: number) => number;
30
32
  export const __wbindgen_export_0: (a: number, b: number) => number;
31
33
  export const __wbindgen_export_1: (a: number, b: number, c: number, d: number) => number;
32
34
  export const __wbindgen_export_2: (a: number) => void;
@@ -5,7 +5,7 @@
5
5
  "Armin Ronacher <armin.ronacher@active-4.com>"
6
6
  ],
7
7
  "description": "a powerful template engine with minimal dependencies",
8
- "version": "2.12.0",
8
+ "version": "2.13.0",
9
9
  "license": "Apache-2.0",
10
10
  "files": [
11
11
  "minijinja_js_bg.wasm",
@@ -46,6 +46,38 @@ const result = env.renderTemplate('index.html', { name: 'World' });
46
46
  console.log(result);
47
47
  ```
48
48
 
49
+ Resolve includes/extends from the filesystem (Node, etc...):
50
+
51
+ ```typescript
52
+ import { Environment } from "minijinja-js";
53
+ import fs from "node:fs";
54
+ import path from "node:path";
55
+
56
+ const env = new Environment();
57
+
58
+ // Resolve relative paths like "./partial.html" against the parent template
59
+ env.setPathJoinCallback((name, parent) => {
60
+ const parentDir = parent ? path.dirname(parent) : process.cwd();
61
+ const joined = path.resolve(parentDir, name);
62
+ return joined.replace(/\\\\/g, '/');
63
+ });
64
+
65
+ // Synchronous loader: return template source or null/undefined if missing
66
+ env.setLoader((name) => {
67
+ try {
68
+ return fs.readFileSync(name, "utf8");
69
+ } catch {
70
+ return null;
71
+ }
72
+ });
73
+
74
+ // Example: main in-memory, include from disk under ./templates/dir/inc.html
75
+ const templatePath = path.resolve(process.cwd(), "templates/dir/main.html");
76
+ env.addTemplate(templatePath, "Hello {% include './inc.html' %}!");
77
+ console.log(env.renderTemplate(templatePath, { value: "World" }));
78
+ // -> Hello [World]!
79
+ ```
80
+
49
81
  Evaluate an expression:
50
82
 
51
83
  ```typescript
@@ -74,6 +106,7 @@ others probably not so much. You might run into the following:
74
106
 
75
107
  * Access of the template engine state from JavaScript is not possible.
76
108
  * You cannot register a custom auto escape callback or a finalizer
109
+ * The loader is synchronous; use sync I/O in Node etc... (e.g. `fs.readFileSync`)
77
110
  * If the engine panics, the WASM runtime corrupts.
78
111
 
79
112
  ## Sponsor
@@ -6,34 +6,46 @@ type UndefinedBehavior = "strict" | "chainable" | "lenient" | "semi_strct";
6
6
  */
7
7
  export class Environment {
8
8
  free(): void;
9
- constructor();
10
9
  /**
11
10
  * Registers a new template by name and source.
12
11
  */
13
12
  addTemplate(name: string, source: string): void;
14
13
  /**
15
- * Removes a template by name.
14
+ * Removes a global again.
16
15
  */
17
- removeTemplate(name: string): void;
16
+ removeGlobal(name: string): void;
18
17
  /**
19
18
  * Clears all templates from the environment.
20
19
  */
21
20
  clearTemplates(): void;
21
+ /**
22
+ * Enables python compatibility.
23
+ */
24
+ enablePyCompat(): void;
25
+ /**
26
+ * Removes a template by name.
27
+ */
28
+ removeTemplate(name: string): void;
29
+ /**
30
+ * Like `renderStr` but with a named template for auto escape detection.
31
+ */
32
+ renderNamedStr(name: string, source: string, ctx: any): string;
22
33
  /**
23
34
  * Renders a registered template by name with the given context.
24
35
  */
25
36
  renderTemplate(name: string, ctx: any): string;
26
37
  /**
27
- * Renders a string template with the given context.
38
+ * Sets a callback to join template paths (for relative includes/extends).
28
39
  *
29
- * This is useful for one-off template rendering without registering the template. The
30
- * template is parsed and rendered immediately.
40
+ * The callback receives `(name, parent)` and should return a joined path string.
41
+ * If it throws or returns a non-string, the original `name` is used.
31
42
  */
32
- renderStr(source: string, ctx: any): string;
43
+ setPathJoinCallback(func: Function): void;
44
+ constructor();
33
45
  /**
34
- * Like `renderStr` but with a named template for auto escape detection.
46
+ * Registers a test function.
35
47
  */
36
- renderNamedStr(name: string, source: string, ctx: any): string;
48
+ addTest(name: string, func: Function): void;
37
49
  /**
38
50
  * Evaluates an expression with the given context.
39
51
  *
@@ -45,26 +57,25 @@ export class Environment {
45
57
  * Registers a filter function.
46
58
  */
47
59
  addFilter(name: string, func: Function): void;
48
- /**
49
- * Registers a test function.
50
- */
51
- addTest(name: string, func: Function): void;
52
- /**
53
- * Enables python compatibility.
54
- */
55
- enablePyCompat(): void;
56
60
  /**
57
61
  * Registers a value as global.
58
62
  */
59
63
  addGlobal(name: string, value: any): void;
60
64
  /**
61
- * Removes a global again.
65
+ * Renders a string template with the given context.
66
+ *
67
+ * This is useful for one-off template rendering without registering the template. The
68
+ * template is parsed and rendered immediately.
62
69
  */
63
- removeGlobal(name: string): void;
70
+ renderStr(source: string, ctx: any): string;
64
71
  /**
65
- * Enables or disables debug mode.
72
+ * Registers a synchronous template loader callback.
73
+ *
74
+ * The provided function is called with a template name and must return a
75
+ * string with the template source or `null`/`undefined` if the template
76
+ * does not exist. Errors thrown are propagated as MiniJinja errors.
66
77
  */
67
- debug: boolean;
78
+ setLoader(func: Function): void;
68
79
  /**
69
80
  * Enables or disables block trimming.
70
81
  */
@@ -73,17 +84,21 @@ export class Environment {
73
84
  * Enables or disables the lstrip blocks feature.
74
85
  */
75
86
  lstripBlocks: boolean;
76
- /**
77
- * Enables or disables keeping of the final newline.
78
- */
79
- keepTrailingNewline: boolean;
80
87
  /**
81
88
  * Reconfigures the behavior of undefined variables.
82
89
  */
83
90
  undefinedBehavior: UndefinedBehavior;
91
+ /**
92
+ * Enables or disables keeping of the final newline.
93
+ */
94
+ keepTrailingNewline: boolean;
84
95
  /**
85
96
  * Configures the max-fuel for template evaluation.
86
97
  */
87
98
  get fuel(): number | undefined;
88
99
  set fuel(value: number | null | undefined);
100
+ /**
101
+ * Enables or disables debug mode.
102
+ */
103
+ debug: boolean;
89
104
  }