-
Notifications
You must be signed in to change notification settings - Fork 0
/
partials.ino
63 lines (53 loc) · 1.68 KB
/
partials.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Ministache.h>
/***************************************************
This is an example of how to use Mustache partials with the Ministache library
(https://github.com/floatplane/ministache).
For more details on Mustache syntax, see http://mustache.github.io/mustache.5.html
****************************************************/
void setup() {
Serial.begin(115200);
Serial.println("");
// Create a JsonDocument instance to hold the data that we'll use in our template
const char* json = R"""(
{
"people": [
{
"name": "Alice",
"role": "Engineer"
},
{
"name": "Bob",
"role": "Intern"
}
]
}
)""";
JsonDocument data;
deserializeJson(data, json);
// Create a template that renders the data for a single person. This is a *partial*.
auto personTemplate = "Name: {{name}}\tRole: {{role}}\n";
// Create a template that renders the data for all people. This is the main template.
// Note that it loops over a data field called "people", and includes the partial "person" for each of
// them.
auto reportTemplate = R"""(
People report:
{{#people}}
{{> person}}
{{/people}}
)""";
// Render the template with the data. The third argument is the partials list. This
// defines how to map a partial reference like "person" to a particular template
// ("personString").
ministache::PartialList partials = {{"person", personTemplate}};
auto output = ministache::render(reportTemplate, data, partials);
// Print the result
Serial.println(output);
// Expected output:
//
// People report:
// Name: Alice Role: Engineer
// Name: Bob Role: Intern
}
void loop() {
delay(500);
}