mikel 0.27.0 → 0.28.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 +39 -3
- package/index.js +32 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -205,7 +205,7 @@ Partials allows you to define custom data. Instead of providing a string with th
|
|
|
205
205
|
- `body`: a string with the partial content.
|
|
206
206
|
- `data`: an object with your custom data for the partial. You can also use `attributes` as an alias.
|
|
207
207
|
|
|
208
|
-
Custom data will be available in the partial content
|
|
208
|
+
Custom data will be available in the partial content in the `@partial.attributes` variable.
|
|
209
209
|
|
|
210
210
|
Example:
|
|
211
211
|
|
|
@@ -213,7 +213,7 @@ Example:
|
|
|
213
213
|
const options = {
|
|
214
214
|
partials: {
|
|
215
215
|
foo: {
|
|
216
|
-
body: "Hello {{@partial.name}}!",
|
|
216
|
+
body: "Hello {{@partial.attributes.name}}!",
|
|
217
217
|
data: {
|
|
218
218
|
name: "Bob",
|
|
219
219
|
},
|
|
@@ -225,6 +225,31 @@ const result = m("{{>foo}}", {}, options);
|
|
|
225
225
|
// Output: 'Hello Bob!'
|
|
226
226
|
```
|
|
227
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
|
+
|
|
228
253
|
### Helpers
|
|
229
254
|
|
|
230
255
|
> Added in `v0.4.0`.
|
|
@@ -440,7 +465,7 @@ console.log(result); // --> "0: John, 1: Alice, 2: Bob,"
|
|
|
440
465
|
|
|
441
466
|
#### Expand helper arguments using the spread operator
|
|
442
467
|
|
|
443
|
-
> This feature was
|
|
468
|
+
> This feature was added in `v0.20.0`.
|
|
444
469
|
|
|
445
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.
|
|
446
471
|
|
|
@@ -465,6 +490,17 @@ const result = m("{{#join ...items ...options}}{{/join}}", data, options);
|
|
|
465
490
|
console.log(result); // --> "John, Alice, Bob"
|
|
466
491
|
```
|
|
467
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
|
+
|
|
468
504
|
### Runtime Variables
|
|
469
505
|
|
|
470
506
|
> Added in `v0.4.0`.
|
package/index.js
CHANGED
|
@@ -110,12 +110,25 @@ const create = (options = {}) => {
|
|
|
110
110
|
i = findClosingToken(tokens, j, t);
|
|
111
111
|
output.push(ctx.helpers[t]({
|
|
112
112
|
args: args,
|
|
113
|
-
opt: opt,
|
|
113
|
+
opt: opt, // deprecated
|
|
114
|
+
options: opt,
|
|
114
115
|
tokens: tokens.slice(j, i),
|
|
115
116
|
data: data,
|
|
116
117
|
variables: vars,
|
|
117
118
|
fn: (blockData = {}, blockVars = {}, blockOutput = []) => {
|
|
118
|
-
|
|
119
|
+
const helperVars = {
|
|
120
|
+
...vars,
|
|
121
|
+
...blockVars,
|
|
122
|
+
helper: {
|
|
123
|
+
name: t,
|
|
124
|
+
options: opt || {},
|
|
125
|
+
args: args || [],
|
|
126
|
+
context: blockData,
|
|
127
|
+
},
|
|
128
|
+
parent: data,
|
|
129
|
+
root: vars.root,
|
|
130
|
+
};
|
|
131
|
+
compile(tokens, blockOutput, blockData, helperVars, j, t);
|
|
119
132
|
return blockOutput.join("");
|
|
120
133
|
},
|
|
121
134
|
}));
|
|
@@ -135,6 +148,16 @@ const create = (options = {}) => {
|
|
|
135
148
|
i = compile(tokens, includeOutput ? output : [], data, vars, i + 1, t);
|
|
136
149
|
}
|
|
137
150
|
}
|
|
151
|
+
else if (tokens[i].startsWith(">*")) {
|
|
152
|
+
const t = tokens[i].slice(2).trim(), partialTokens = tokens.slice(i + 1);
|
|
153
|
+
const lastIndex = partialTokens.findIndex((token, j) => {
|
|
154
|
+
return j % 2 !== 0 && token.trim().startsWith("/") && token.trim().endsWith(t);
|
|
155
|
+
});
|
|
156
|
+
if (typeof ctx.partials[t] === "undefined") {
|
|
157
|
+
ctx.partials[t] = untokenize(partialTokens.slice(0, lastIndex));
|
|
158
|
+
}
|
|
159
|
+
i = i + lastIndex + 1;
|
|
160
|
+
}
|
|
138
161
|
else if (tokens[i].startsWith(">")) {
|
|
139
162
|
const [t, args, opt] = parseArgs(tokens[i].replace(/^>{1,2}/, ""), data, vars);
|
|
140
163
|
const blockContent = []; // to store partial block content
|
|
@@ -146,7 +169,13 @@ const create = (options = {}) => {
|
|
|
146
169
|
const newVars = {
|
|
147
170
|
...vars,
|
|
148
171
|
content: blockContent.join(""),
|
|
149
|
-
partial:
|
|
172
|
+
partial: {
|
|
173
|
+
name: t,
|
|
174
|
+
attributes: ctx.partials[t]?.attributes || ctx.partials[t]?.data || {},
|
|
175
|
+
args: args || [],
|
|
176
|
+
options: opt || {},
|
|
177
|
+
context: newData,
|
|
178
|
+
},
|
|
150
179
|
};
|
|
151
180
|
compile(tokenize(ctx.partials[t]?.body || ctx.partials[t]), output, newData, newVars, 0, "");
|
|
152
181
|
}
|