You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Custom helpers should be provided as an object in the `options.helpers` field, where each key represents the name of the helper and the corresponding value is a function defining the helper's behavior.
244
245
@@ -251,8 +252,8 @@ const data = {
251
252
};
252
253
constoptions= {
253
254
helpers: {
254
-
customHelper:value=> {
255
-
return`Hello, ${value}!`;
255
+
customHelper:params=> {
256
+
return`Hello, ${params.args[0]}!`;
256
257
},
257
258
},
258
259
};
@@ -261,8 +262,10 @@ const result = m(template, data, options);
261
262
console.log(result); // Output: "Hello, World!"
262
263
```
263
264
264
-
Custom helper functions receive multiple arguments, where the first N arguments are the variables with the helper is called in the template, and the last argument is an options object containing the following keys:
265
+
Custom helper functions receive a single object as argument, containing the following keys:
265
266
267
+
-`args`: an array containing the variables with the helper is called in the template.
268
+
-`opt`: an object containing the keyword arguments provided to the helper.
266
269
-`context`: the current context (data) where the helper has been executed.
267
270
-`fn`: a function that executes the template provided in the helper block and returns a string with the evaluated template in the provided context.
@@ -343,11 +346,18 @@ The `@last` variable allows to check if the current iteration using the `#each`
343
346
### Functions
344
347
345
348
> Added in `v0.8.0`.
349
+
> Breaking change introduced in `v0.12.0`.
346
350
347
351
Mikel allows users to define custom functions that can be used within templates to perform dynamic operations. Functions can be invoked in the template using the `=` character, followed by the function name and the variables to be provided to the function. Variables should be separated by spaces.
348
352
349
353
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.
350
354
355
+
Functions will receive a single object as argument, containing the following keys:
356
+
357
+
-`args`: an array containing the variables with the function is called in the template.
358
+
-`opt`: an object containing the keyword arguments provided to the function.
359
+
-`context`: the current context (data) where the function has been executed.
360
+
351
361
Example:
352
362
353
363
```javascript
@@ -359,8 +369,8 @@ const data = {
359
369
};
360
370
constoptions= {
361
371
functions: {
362
-
fullName: (firstName, lastName) => {
363
-
return`${firstName}${lastName}`;
372
+
fullName: ({args}) => {
373
+
return`${args[0]}${args[1]}`;
364
374
}
365
375
},
366
376
};
@@ -369,8 +379,6 @@ const result = m("My name is: {{=fullName user.firstName user.lastName}}", data,
369
379
console.log(result); // --> "My name is: John Doe"
370
380
```
371
381
372
-
In this example, the custom function `fullName` is defined to take two arguments, `firstName` and `lastName`, and return the full name. The template then uses this function to concatenate and render the full name.
0 commit comments