mikel 0.3.0 → 0.3.1

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 +19 -0
  2. package/index.js +2 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -87,6 +87,25 @@ const result = m("{{> hello}}", data, {partials});
87
87
  // Output: 'Hello Bob!'
88
88
  ```
89
89
 
90
+ #### Custom context in partials (added in v0.3.1)
91
+
92
+ You can provide a custom context for the partial by specifying a field of the data: `{{> partialName dataField}}`.
93
+
94
+ ```javascript
95
+ const data = {
96
+ currentUser: {
97
+ name: "John Doe",
98
+ email: "john@example.com",
99
+ },
100
+ };
101
+ const partials = {
102
+ user: "{{name}} <{{email}}>",
103
+ };
104
+
105
+ const result = m("User: {{> user currentUser}}", data, {partials});
106
+ // Output: 'User: John Doe <john@example.com>'
107
+ ```
108
+
90
109
  ## API
91
110
 
92
111
  ### `m(template, data[, options])`
package/index.js CHANGED
@@ -40,9 +40,9 @@ const compile = (tokens, output, ctx, opt, index, section) => {
40
40
  }
41
41
  }
42
42
  else if (tokens[i].startsWith(">")) {
43
- const t = tokens[i].slice(1).trim();
43
+ const [t, v] = tokens[i].slice(1).trim().split(" ");
44
44
  if (typeof opt?.partials?.[t] === "string") {
45
- compile(opt.partials[t].split(tags), output, ctx, opt, 0, "");
45
+ compile(opt.partials[t].split(tags), output, v ? get(ctx, v) : ctx, opt, 0, "");
46
46
  }
47
47
  }
48
48
  else if (tokens[i].startsWith("/")) {
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.3.0",
4
+ "version": "0.3.1",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",