mikel 0.26.2 → 0.27.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.
- package/README.md +21 -2
- package/index.js +12 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,6 +36,18 @@ const result = m(`Hello {{name || "World"}}!`, {});
|
|
|
36
36
|
// Output: 'Hello World!'
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
### Comments
|
|
40
|
+
|
|
41
|
+
> This feature was added in `v0.27.0`.
|
|
42
|
+
|
|
43
|
+
Any content between `{{!--` and `--}}` will be completely ignored during template rendering. Comments can span multiple lines and are not included in the output or parsed AST.
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
{{!-- This is a comment --}}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
> **Note**: Nested comments are not supported. The first closing `--}}` encountered will terminate the comment block.
|
|
50
|
+
|
|
39
51
|
### Sections
|
|
40
52
|
|
|
41
53
|
Sections allow for conditional rendering of blocks of content based on the presence or absence of a value in the data object. Use the pound symbol `#` to start a section and the caret `^` to denote an inverted section. End the section with a forward slash `/`.
|
|
@@ -100,6 +112,13 @@ const result = m("{{> hello}}", data, {partials});
|
|
|
100
112
|
// Output: 'Hello Bob!'
|
|
101
113
|
```
|
|
102
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
|
+
|
|
103
122
|
#### Custom context in partials
|
|
104
123
|
|
|
105
124
|
> This feature was added in `v0.3.1`.
|
|
@@ -193,7 +212,7 @@ Partials allows you to define custom data. Instead of providing a string with th
|
|
|
193
212
|
- `body`: a string with the partial content.
|
|
194
213
|
- `data`: an object with your custom data for the partial. You can also use `attributes` as an alias.
|
|
195
214
|
|
|
196
|
-
Custom data will be available in the partial content
|
|
215
|
+
Custom data will be available in the partial content in the `@partial.attributes` variable.
|
|
197
216
|
|
|
198
217
|
Example:
|
|
199
218
|
|
|
@@ -201,7 +220,7 @@ Example:
|
|
|
201
220
|
const options = {
|
|
202
221
|
partials: {
|
|
203
222
|
foo: {
|
|
204
|
-
body: "Hello {{@partial.name}}!",
|
|
223
|
+
body: "Hello {{@partial.attributes.name}}!",
|
|
205
224
|
data: {
|
|
206
225
|
name: "Bob",
|
|
207
226
|
},
|
package/index.js
CHANGED
|
@@ -16,8 +16,12 @@ const get = (data, path = ".") => {
|
|
|
16
16
|
return path.replace(/^this\./, "").split(".").reduce((x, k) => x?.[k], data) ?? "";
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
// @description tokenize
|
|
20
|
-
const tokenize = (str = "") =>
|
|
19
|
+
// @description tokenize the provided string
|
|
20
|
+
const tokenize = (str = "") => {
|
|
21
|
+
return str.replace(/\{\{!--[\s\S]*?--}}/g, "").split(/\{\{|\}\}/);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// @description untokenize the provided tokens array and get a string
|
|
21
25
|
const untokenize = (ts = [], s = "{{", e = "}}") => {
|
|
22
26
|
return ts.length > 0 ? ts.reduce((p, t, i) => p + (i % 2 === 0 ? e : s) + t) : "";
|
|
23
27
|
};
|
|
@@ -142,7 +146,12 @@ const create = (options = {}) => {
|
|
|
142
146
|
const newVars = {
|
|
143
147
|
...vars,
|
|
144
148
|
content: blockContent.join(""),
|
|
145
|
-
partial:
|
|
149
|
+
partial: {
|
|
150
|
+
name: t,
|
|
151
|
+
attributes: ctx.partials[t]?.attributes || ctx.partials[t]?.data || {},
|
|
152
|
+
args: args || [],
|
|
153
|
+
opt: opt || {},
|
|
154
|
+
},
|
|
146
155
|
};
|
|
147
156
|
compile(tokenize(ctx.partials[t]?.body || ctx.partials[t]), output, newData, newVars, 0, "");
|
|
148
157
|
}
|