You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Zero-dependency back-tick templates using property names instead of expressions.
10
+
Part of the [Metarhia](https://github.com/metarhia) stack.
11
+
12
+
## Installation
10
13
11
-
- Install: `npm install tickplate`
12
-
- Require: `const t = require('tickplate');`
13
-
- Place tag `t` before templated string
14
+
```bash
15
+
npm install tickplate
16
+
```
14
17
15
-
## Examples:
18
+
## Usage
19
+
20
+
Place tag `t` before a template literal. Placeholders use property names (strings)
21
+
as keys; pass a data object to the returned function to render.
16
22
17
23
```js
18
24
constt=require('tickplate');
@@ -31,18 +37,58 @@ const data = {
31
37
consttempl= t`${'hello'}${'myFriend'}, great ${'positions'} of Rome`;
32
38
33
39
console.log(templ(data));
40
+
// Ave! Marcus Aurelius, great emperor, philosopher, writer of Rome
41
+
```
42
+
43
+
### ESM
44
+
45
+
```js
46
+
importtfrom'tickplate';
47
+
48
+
consttempl= t`Hello ${'name'}!`;
49
+
console.log(templ({ name:'World' }));
50
+
// Hello World!
51
+
```
52
+
53
+
## API
54
+
55
+
### `t(strings, ...keys)`
56
+
57
+
Tagged template literal. Returns a function `(values, opts?) => string`.
58
+
59
+
-**Placeholders**: `${'key'}` — property name from the data object.
60
+
-**Default values**: `${'key=value'}` — JSON-parsable default when the key is
61
+
missing. Example: `${'greeting="Hello"'}` or `${'count=0'}`.
62
+
-**Arrays**: Values are joined with `,` by default. Use `opts.delimiter` to
63
+
customize (e.g. `{ delimiter: ', ' }`).
64
+
65
+
### `templ(values, opts?)`
66
+
67
+
Renders the template.
68
+
69
+
-**values**: Data object. Keys not present render as empty string. `null` and
70
+
`undefined` are treated as `{}`.
71
+
-**opts.delimiter**: String (or coercible value) used to join array elements.
72
+
Default: `','`.
73
+
74
+
## Examples
75
+
76
+
### With delimiter
77
+
78
+
```js
34
79
console.log(templ(data, { delimiter:', ' }));
80
+
// Ave! Marcus Aurelius, great emperor, philosopher, writer of Rome
35
81
```
36
82
37
-
With default values provided (optionally):
83
+
### With default values
38
84
39
85
```js
40
-
constt=require('tickplate');
86
+
consttempl= t`${'greeting='}${'person="Marcus Aurelius"'}, great ${'positions=["emperor", "philosopher"]'} of Rome from ${'ruleFrom=161'} to ${'ruleTo=180'} AD`;
41
87
42
88
constdata= {
43
89
greeting:'Valē!',
44
90
person: {
45
-
name:'Lucius Aurelius Verus',
91
+
name:'Lucius Verus',
46
92
toString() {
47
93
returnthis.name;
48
94
},
@@ -52,13 +98,12 @@ const data = {
52
98
ruleTo:169,
53
99
};
54
100
55
-
consttempl= t`${'greeting='}${'person="Marcus Aurelius"'}, great ${'positions=["emperor", "philosopher"]'} of Rome from ${'ruleFrom=161'} to ${'ruleTo=180'} AD`;
56
-
57
101
console.log(templ(data));
102
+
// Valē! Lucius Verus, great brother,emperor,co-emperor of Rome from 161 to 180 AD
0 commit comments