mikel 0.12.0 → 0.14.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 +32 -0
- package/index.js +12 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,17 @@ Mikel supports the following syntax for rendering templates:
|
|
|
25
25
|
|
|
26
26
|
Use double curly braces `{{ }}` to insert variables into your template. Variables will be replaced with the corresponding values from the data object.
|
|
27
27
|
|
|
28
|
+
#### Fallback values
|
|
29
|
+
|
|
30
|
+
> Added in `v0.14.0`.
|
|
31
|
+
|
|
32
|
+
You can specify a value as a fallback, using the double OR `||` operator and followed by the fallback value.
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const result = m(`Hello {{name || "World"}}!`, {});
|
|
36
|
+
// Output: 'Hello World!'
|
|
37
|
+
```
|
|
38
|
+
|
|
28
39
|
### Sections
|
|
29
40
|
|
|
30
41
|
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 `/`.
|
|
@@ -110,6 +121,27 @@ const result = m("User: {{> user currentUser}}", data, {partials});
|
|
|
110
121
|
// Output: 'User: John Doe <john@example.com>'
|
|
111
122
|
```
|
|
112
123
|
|
|
124
|
+
#### Keyword arguments in partials
|
|
125
|
+
|
|
126
|
+
> This feature was added in `v0.13.0`.
|
|
127
|
+
|
|
128
|
+
You can provide keyword arguments in partials to generate a new context object using the provided keywords.
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
const data = {
|
|
132
|
+
name: "John Doe",
|
|
133
|
+
email: "john@example.com",
|
|
134
|
+
};
|
|
135
|
+
const partials = {
|
|
136
|
+
user: "{{userName}} <{{userEmail}}>",
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const result = m("User: {{>user userName=name userEmail=email }}", data, {partials});
|
|
140
|
+
// Output: 'User: John Doe <john@example.com>'
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Please note that providing keyword arguments and a custom context to a partial is not supported. On this situation, the partial will be evaluated only with the custom context.
|
|
144
|
+
|
|
113
145
|
### Built-in helpers
|
|
114
146
|
|
|
115
147
|
> Added in `v0.4.0`.
|
package/index.js
CHANGED
|
@@ -71,12 +71,6 @@ const create = (template = "", options = {}) => {
|
|
|
71
71
|
if (i % 2 === 0) {
|
|
72
72
|
output.push(tokens[i]);
|
|
73
73
|
}
|
|
74
|
-
else if (tokens[i].startsWith("@")) {
|
|
75
|
-
output.push(get(vars, tokens[i].slice(1).trim() ?? "_") ?? "");
|
|
76
|
-
}
|
|
77
|
-
else if (tokens[i].startsWith("!")) {
|
|
78
|
-
output.push(get(context, tokens[i].slice(1).trim()));
|
|
79
|
-
}
|
|
80
74
|
else if (tokens[i].startsWith("#") && typeof helpers[tokens[i].slice(1).trim().split(" ")[0]] === "function") {
|
|
81
75
|
const [t, args, opt] = parseArgs(tokens[i].slice(1), context, vars);
|
|
82
76
|
const j = i + 1;
|
|
@@ -110,9 +104,10 @@ const create = (template = "", options = {}) => {
|
|
|
110
104
|
}
|
|
111
105
|
}
|
|
112
106
|
else if (tokens[i].startsWith(">")) {
|
|
113
|
-
const [t,
|
|
107
|
+
const [t, args, opt] = parseArgs(tokens[i].slice(1), context, vars);
|
|
114
108
|
if (typeof partials[t] === "string") {
|
|
115
|
-
|
|
109
|
+
const newCtx = args.length > 0 ? args[0] : (Object.keys(opt).length > 0 ? opt : context);
|
|
110
|
+
compile(partials[t].split(tags), output, newCtx, vars, 0, "");
|
|
116
111
|
}
|
|
117
112
|
}
|
|
118
113
|
else if (tokens[i].startsWith("=")) {
|
|
@@ -128,7 +123,15 @@ const create = (template = "", options = {}) => {
|
|
|
128
123
|
break;
|
|
129
124
|
}
|
|
130
125
|
else {
|
|
131
|
-
|
|
126
|
+
const t = tokens[i].split("||").map(v => {
|
|
127
|
+
// check if the returned value should not be escaped
|
|
128
|
+
if (v.trim().startsWith("!")) {
|
|
129
|
+
return parse(v.trim().slice(1).trim(), context, vars);
|
|
130
|
+
}
|
|
131
|
+
// escape the returned value
|
|
132
|
+
return escape(parse(v.trim(), context, vars));
|
|
133
|
+
});
|
|
134
|
+
output.push(t.find(v => !!v) ?? "");
|
|
132
135
|
}
|
|
133
136
|
i = i + 1;
|
|
134
137
|
}
|