mikel 0.13.0 → 0.14.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 +11 -0
- package/index.js +9 -7
- package/package.json +2 -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 `/`.
|
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;
|
|
@@ -129,7 +123,15 @@ const create = (template = "", options = {}) => {
|
|
|
129
123
|
break;
|
|
130
124
|
}
|
|
131
125
|
else {
|
|
132
|
-
|
|
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) ?? "");
|
|
133
135
|
}
|
|
134
136
|
i = i + 1;
|
|
135
137
|
}
|
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.
|
|
4
|
+
"version": "0.14.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Josemi Juanes",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"./package.json": "./package.json"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
|
+
"release": "node scripts/release.js",
|
|
20
21
|
"test": "node test.js"
|
|
21
22
|
},
|
|
22
23
|
"keywords": [
|