mikel 0.27.1 → 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.
Files changed (3) hide show
  1. package/README.md +37 -8
  2. package/index.js +27 -3
  3. 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`.
@@ -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 addedin `v0.20.0`.
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`.
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
- compile(tokens, blockOutput, blockData, {...vars, ...blockVars, parent: data, root: vars.root}, j, t);
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
@@ -150,7 +173,8 @@ const create = (options = {}) => {
150
173
  name: t,
151
174
  attributes: ctx.partials[t]?.attributes || ctx.partials[t]?.data || {},
152
175
  args: args || [],
153
- opt: opt || {},
176
+ options: opt || {},
177
+ context: newData,
154
178
  },
155
179
  };
156
180
  compile(tokenize(ctx.partials[t]?.body || ctx.partials[t]), output, newData, newVars, 0, "");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel",
3
3
  "description": "Micro templating library with zero dependencies",
4
- "version": "0.27.1",
4
+ "version": "0.28.0",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",