mikel 0.27.1 → 0.29.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.
- package/README.md +42 -13
- package/index.d.ts +4 -2
- package/index.js +39 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,13 +112,6 @@ const result = m("{{> hello}}", data, {partials});
|
|
|
112
112
|
// Output: 'Hello Bob!'
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
Partial metadata can be accessed using the `@partial` variable inside the partial. It contains the following fields:
|
|
116
|
-
|
|
117
|
-
- `name`: the name of the partial being rendered.
|
|
118
|
-
- `args`: an array containing the positional arguments provided to the partial (if any). See the **Custom context in partials** section for more details.
|
|
119
|
-
- `opt`: an object containing the keyword arguments provided to the partial (if any). See the **Keyword arguments in partials** section for more details.
|
|
120
|
-
- `attributes`: the custom data provided to the partial (if any). See the **Partials data** section for more details.
|
|
121
|
-
|
|
122
115
|
#### Custom context in partials
|
|
123
116
|
|
|
124
117
|
> This feature was added in `v0.3.1`.
|
|
@@ -232,6 +225,31 @@ const result = m("{{>foo}}", {}, options);
|
|
|
232
225
|
// Output: 'Hello Bob!'
|
|
233
226
|
```
|
|
234
227
|
|
|
228
|
+
#### Accessing to partial metadata using the `@partial` variable
|
|
229
|
+
|
|
230
|
+
> Added in `v0.28.0`.
|
|
231
|
+
|
|
232
|
+
Partial metadata can be accessed using the `@partial` variable inside the partial. It contains the following fields:
|
|
233
|
+
|
|
234
|
+
- `@partial.name`: the name of the partial being rendered.
|
|
235
|
+
- `@partial.args`: an array containing the positional arguments provided to the partial (if any).
|
|
236
|
+
- `@partial.options`: an object containing the keyword arguments provided to the partial (if any).
|
|
237
|
+
- `@partial.attributes`: the custom data provided to the partial (if any).
|
|
238
|
+
- `@partial.context`: the current rendering context.
|
|
239
|
+
|
|
240
|
+
### Inline partials
|
|
241
|
+
|
|
242
|
+
> Added in `v0.28.0`.
|
|
243
|
+
|
|
244
|
+
Inline partials allows you to define partials directly in your template. Use `>*` followed by the partial name to start the partial definition, and end the partial definition with a slash `/` followed by the partial name. For example, `{{>*foo}}` begins a partial definition called `foo`, and `{{/foo}}` ends it.
|
|
245
|
+
|
|
246
|
+
Example:
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
const result = m(`{{>*foo}}Hello {{name}}!{{/foo}}{{>foo name="Bob"}}`, {});
|
|
250
|
+
// Output: 'Hello Bob!'
|
|
251
|
+
```
|
|
252
|
+
|
|
235
253
|
### Helpers
|
|
236
254
|
|
|
237
255
|
> Added in `v0.4.0`.
|
|
@@ -415,10 +433,10 @@ const result = m(template, data, options);
|
|
|
415
433
|
console.log(result); // Output: "Hello, World!"
|
|
416
434
|
```
|
|
417
435
|
|
|
418
|
-
Custom helper functions receive a single object as argument, containing the following
|
|
436
|
+
Custom helper functions receive a single `params` object as argument, containing the following fields:
|
|
419
437
|
|
|
420
438
|
- `args`: an array containing the variables with the helper is called in the template.
|
|
421
|
-
- `
|
|
439
|
+
- `options`: an object containing the keyword arguments provided to the helper.
|
|
422
440
|
- `data`: the current data where the helper has been executed.
|
|
423
441
|
- `variables`: an object containing the runtime variables available in the current context (e.g., `@root`, `@index`, etc.).
|
|
424
442
|
- `fn`: a function that executes the template provided in the helper block and returns a string with the evaluated template in the provided context.
|
|
@@ -447,7 +465,7 @@ console.log(result); // --> "0: John, 1: Alice, 2: Bob,"
|
|
|
447
465
|
|
|
448
466
|
#### Expand helper arguments using the spread operator
|
|
449
467
|
|
|
450
|
-
> This feature was
|
|
468
|
+
> This feature was added in `v0.20.0`.
|
|
451
469
|
|
|
452
470
|
You can use the spread operator `...` to expand the arguments of a helper. This allows you to pass an array of values as individual arguments to the helper, or to pass an object as keyword arguments.
|
|
453
471
|
|
|
@@ -472,6 +490,17 @@ const result = m("{{#join ...items ...options}}{{/join}}", data, options);
|
|
|
472
490
|
console.log(result); // --> "John, Alice, Bob"
|
|
473
491
|
```
|
|
474
492
|
|
|
493
|
+
#### Accessing to helper metadata using the `@helper` variable
|
|
494
|
+
|
|
495
|
+
> Introduced in `v0.28.0`.
|
|
496
|
+
|
|
497
|
+
Inside any helper block, you can access metadata about the current invocation through the `@helper` variable. It exposes the following fields:
|
|
498
|
+
|
|
499
|
+
- `@helper.name`: the name of the helper being invoked.
|
|
500
|
+
- `@helper.args`: an array of positional arguments passed to the helper.
|
|
501
|
+
- `@helper.options`: an object containing named (key-value) arguments.
|
|
502
|
+
- `@helper.context`: the current rendering context.
|
|
503
|
+
|
|
475
504
|
### Runtime Variables
|
|
476
505
|
|
|
477
506
|
> Added in `v0.4.0`.
|
|
@@ -533,10 +562,10 @@ Mikel allows users to define custom functions that can be used within templates
|
|
|
533
562
|
|
|
534
563
|
Functions should be provided in the `options.functions` field of the options object when rendering a template. Each function is defined by a name and a corresponding function that performs the desired operation.
|
|
535
564
|
|
|
536
|
-
Functions will receive a single object as argument, containing the following keys:
|
|
565
|
+
Functions will receive a single `params` object as argument, containing the following keys:
|
|
537
566
|
|
|
538
567
|
- `args`: an array containing the variables with the function is called in the template.
|
|
539
|
-
- `
|
|
568
|
+
- `options`: an object containing the keyword arguments provided to the function.
|
|
540
569
|
- `data`: the current data object where the function has been executed.
|
|
541
570
|
- `variables`: an object containing the runtime variables available in the current context (e.g., `@root`, `@index`, etc.).
|
|
542
571
|
|
|
@@ -603,7 +632,7 @@ const data = {
|
|
|
603
632
|
const options = {
|
|
604
633
|
functions: {
|
|
605
634
|
fullName: params => {
|
|
606
|
-
return `${params.
|
|
635
|
+
return `${params.options.firstName} ${params.options.lastName}`;
|
|
607
636
|
}
|
|
608
637
|
},
|
|
609
638
|
};
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type MikelHelper = (params: {
|
|
2
2
|
args: any[];
|
|
3
|
-
opt
|
|
3
|
+
opt?: Record<string, any>;
|
|
4
|
+
options: Record<string, any>;
|
|
4
5
|
tokens: string[];
|
|
5
6
|
data: Record<string, any>;
|
|
6
7
|
variables: Record<string, any>;
|
|
@@ -14,7 +15,8 @@ export type MikelPartial = {
|
|
|
14
15
|
|
|
15
16
|
export type MikelFunction = (params: {
|
|
16
17
|
args: any[];
|
|
17
|
-
opt
|
|
18
|
+
opt?: Record<string, any>;
|
|
19
|
+
options: Record<string,any>;
|
|
18
20
|
data: Record<string, any>;
|
|
19
21
|
variables: Record<string, any>;
|
|
20
22
|
}) => string | void;
|
package/index.js
CHANGED
|
@@ -75,9 +75,17 @@ const findClosingToken = (tokens, i, token) => {
|
|
|
75
75
|
const defaultHelpers = {
|
|
76
76
|
"each": p => {
|
|
77
77
|
const items = typeof p.args[0] === "object" ? Object.entries(p.args[0] || {}) : [];
|
|
78
|
-
const limit = Math.min(items.length - (p.
|
|
79
|
-
return items.slice(p.
|
|
80
|
-
.map((item, index) =>
|
|
78
|
+
const limit = Math.min(items.length - (p.options?.skip || 0), p.options?.limit || items.length);
|
|
79
|
+
return items.slice(p.options?.skip || 0, (p.options?.skip || 0) + limit)
|
|
80
|
+
.map((item, index) => {
|
|
81
|
+
return p.fn(item[1], {
|
|
82
|
+
index: index,
|
|
83
|
+
key: item[0],
|
|
84
|
+
value: item[1],
|
|
85
|
+
first: index === 0,
|
|
86
|
+
last: index === items.length - 1,
|
|
87
|
+
});
|
|
88
|
+
})
|
|
81
89
|
.join("");
|
|
82
90
|
},
|
|
83
91
|
"if": p => !!p.args[0] ? p.fn(p.data) : "",
|
|
@@ -110,12 +118,25 @@ const create = (options = {}) => {
|
|
|
110
118
|
i = findClosingToken(tokens, j, t);
|
|
111
119
|
output.push(ctx.helpers[t]({
|
|
112
120
|
args: args,
|
|
113
|
-
opt: opt,
|
|
121
|
+
opt: opt, // deprecated
|
|
122
|
+
options: opt,
|
|
114
123
|
tokens: tokens.slice(j, i),
|
|
115
124
|
data: data,
|
|
116
125
|
variables: vars,
|
|
117
126
|
fn: (blockData = {}, blockVars = {}, blockOutput = []) => {
|
|
118
|
-
|
|
127
|
+
const helperVars = {
|
|
128
|
+
...vars,
|
|
129
|
+
...blockVars,
|
|
130
|
+
helper: {
|
|
131
|
+
name: t,
|
|
132
|
+
options: opt || {},
|
|
133
|
+
args: args || [],
|
|
134
|
+
context: blockData,
|
|
135
|
+
},
|
|
136
|
+
parent: data,
|
|
137
|
+
root: vars.root,
|
|
138
|
+
};
|
|
139
|
+
compile(tokens, blockOutput, blockData, helperVars, j, t);
|
|
119
140
|
return blockOutput.join("");
|
|
120
141
|
},
|
|
121
142
|
}));
|
|
@@ -135,6 +156,16 @@ const create = (options = {}) => {
|
|
|
135
156
|
i = compile(tokens, includeOutput ? output : [], data, vars, i + 1, t);
|
|
136
157
|
}
|
|
137
158
|
}
|
|
159
|
+
else if (tokens[i].startsWith(">*")) {
|
|
160
|
+
const t = tokens[i].slice(2).trim(), partialTokens = tokens.slice(i + 1);
|
|
161
|
+
const lastIndex = partialTokens.findIndex((token, j) => {
|
|
162
|
+
return j % 2 !== 0 && token.trim().startsWith("/") && token.trim().endsWith(t);
|
|
163
|
+
});
|
|
164
|
+
if (typeof ctx.partials[t] === "undefined") {
|
|
165
|
+
ctx.partials[t] = untokenize(partialTokens.slice(0, lastIndex));
|
|
166
|
+
}
|
|
167
|
+
i = i + lastIndex + 1;
|
|
168
|
+
}
|
|
138
169
|
else if (tokens[i].startsWith(">")) {
|
|
139
170
|
const [t, args, opt] = parseArgs(tokens[i].replace(/^>{1,2}/, ""), data, vars);
|
|
140
171
|
const blockContent = []; // to store partial block content
|
|
@@ -150,7 +181,8 @@ const create = (options = {}) => {
|
|
|
150
181
|
name: t,
|
|
151
182
|
attributes: ctx.partials[t]?.attributes || ctx.partials[t]?.data || {},
|
|
152
183
|
args: args || [],
|
|
153
|
-
|
|
184
|
+
options: opt || {},
|
|
185
|
+
context: newData,
|
|
154
186
|
},
|
|
155
187
|
};
|
|
156
188
|
compile(tokenize(ctx.partials[t]?.body || ctx.partials[t]), output, newData, newVars, 0, "");
|
|
@@ -159,7 +191,7 @@ const create = (options = {}) => {
|
|
|
159
191
|
else if (tokens[i].startsWith("=")) {
|
|
160
192
|
const [t, args, opt] = parseArgs(tokens[i].slice(1), data, vars);
|
|
161
193
|
if (typeof ctx.functions[t] === "function") {
|
|
162
|
-
output.push(ctx.functions[t]({args, opt, data, variables: vars}) || "");
|
|
194
|
+
output.push(ctx.functions[t]({args, opt, options: opt, data, variables: vars}) || "");
|
|
163
195
|
}
|
|
164
196
|
}
|
|
165
197
|
else if (tokens[i].startsWith("/")) {
|