mikel 0.17.0 → 0.18.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 +29 -0
- package/index.d.ts +48 -0
- package/index.js +7 -3
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -161,6 +161,35 @@ const result = m("{{>>foo}}Bob{{/foo}}", {}, options);
|
|
|
161
161
|
// Output: 'Hello Bob!'
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
+
#### Partials data
|
|
165
|
+
|
|
166
|
+
> This feature was added in `v0.18.0`.
|
|
167
|
+
|
|
168
|
+
Partials allows you to define custom data. Instead of providing a string with the partial content, you can provide an object with the following keys:
|
|
169
|
+
|
|
170
|
+
- `body`: a string with the partial content.
|
|
171
|
+
- `data`: an object with your custom data for the partial. You can also use `attributes` as an alias.
|
|
172
|
+
|
|
173
|
+
Custom data will be available in the partial content as a variable `@partial`.
|
|
174
|
+
|
|
175
|
+
Example:
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
const options = {
|
|
179
|
+
partials: {
|
|
180
|
+
foo: {
|
|
181
|
+
body: "Hello {{@partial.name}}!",
|
|
182
|
+
data: {
|
|
183
|
+
name: "Bob",
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const result = m("{{> foo}}", {}, options);
|
|
190
|
+
// Output: 'Hello Bob!'
|
|
191
|
+
```
|
|
192
|
+
|
|
164
193
|
### Inline partials
|
|
165
194
|
|
|
166
195
|
> This feature was added in `v0.16.0`.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
declare type HelperFunction = (params: {
|
|
2
|
+
args: any[];
|
|
3
|
+
opt: Record<string, any>;
|
|
4
|
+
context: Record<string, any>;
|
|
5
|
+
fn: (context?: Record<string, any>, vars?: Record<string, any>, output?: string[]) => string;
|
|
6
|
+
}) => string;
|
|
7
|
+
|
|
8
|
+
declare interface Helpers {
|
|
9
|
+
[key: string]: HelperFunction;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare interface Partials {
|
|
13
|
+
[key: string]: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare interface Functions {
|
|
17
|
+
[key: string]: (params: {
|
|
18
|
+
args: any[];
|
|
19
|
+
opt: Record<string, any>;
|
|
20
|
+
context: Record<string, any>;
|
|
21
|
+
}) => string | void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare interface MikelOptions {
|
|
25
|
+
helpers?: Helpers;
|
|
26
|
+
partials?: Partials;
|
|
27
|
+
functions?: Functions;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare interface MikelTemplate {
|
|
31
|
+
(data?: any): string;
|
|
32
|
+
addHelper(name: string, fn: HelperFunction): void;
|
|
33
|
+
removeHelper(name: string): void;
|
|
34
|
+
addFunction(name: string, fn: (params: any) => string | void): void;
|
|
35
|
+
removeFunction(name: string): void;
|
|
36
|
+
addPartial(name: string, partial: string): void;
|
|
37
|
+
removePartial(name: string): void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare const mikel: {
|
|
41
|
+
(template: string, data?: any, options?: Partial<MikelOptions>): string;
|
|
42
|
+
create(template: string, options?: Partial<MikelOptions>): MikelTemplate;
|
|
43
|
+
escape(str: string): string;
|
|
44
|
+
get(context: any, path: string): any;
|
|
45
|
+
parse(value: string, context?: any, vars?: any): any;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default mikel;
|
package/index.js
CHANGED
|
@@ -124,10 +124,14 @@ const create = (template = "", options = {}) => {
|
|
|
124
124
|
if (tokens[i].startsWith(">>")) {
|
|
125
125
|
i = compile(tokens, blockContent, context, vars, i + 1, t);
|
|
126
126
|
}
|
|
127
|
-
if (typeof partials[t] === "string") {
|
|
127
|
+
if (typeof partials[t] === "string" || typeof partials[t]?.body === "string") {
|
|
128
128
|
const newCtx = args.length > 0 ? args[0] : (Object.keys(opt).length > 0 ? opt : context);
|
|
129
|
-
const newVars = {
|
|
130
|
-
|
|
129
|
+
const newVars = {
|
|
130
|
+
...vars,
|
|
131
|
+
content: blockContent.join(""),
|
|
132
|
+
partial: partials[t]?.attributes || partials[t]?.data || {},
|
|
133
|
+
};
|
|
134
|
+
compile(tokenize(partials[t]?.body || partials[t]), output, newCtx, newVars, 0, "");
|
|
131
135
|
}
|
|
132
136
|
}
|
|
133
137
|
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.
|
|
4
|
+
"version": "0.18.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Josemi Juanes",
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
"bugs": "https://github.com/jmjuanes/mikel/issues",
|
|
13
13
|
"main": "index.js",
|
|
14
14
|
"module": "index.js",
|
|
15
|
+
"types": "index.d.ts",
|
|
15
16
|
"exports": {
|
|
16
17
|
".": "./index.js",
|
|
17
18
|
"./package.json": "./package.json"
|
|
18
19
|
},
|
|
19
20
|
"scripts": {
|
|
20
|
-
"release": "node scripts/release.js",
|
|
21
21
|
"test": "node test.js"
|
|
22
22
|
},
|
|
23
23
|
"keywords": [
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"files": [
|
|
28
28
|
"README.md",
|
|
29
29
|
"LICENSE",
|
|
30
|
-
"index.js"
|
|
30
|
+
"index.js",
|
|
31
|
+
"index.d.ts"
|
|
31
32
|
]
|
|
32
33
|
}
|