Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
floatplane committed Mar 16, 2024
1 parent f1c3fe3 commit 1f6a70f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/basic/basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
****************************************************/

void setup() {
Serial.begin(9600);
Serial.begin(115200);
Serial.println("");

// Create a JSON object to hold the data that we'll use in our template
JsonDocument data;
Expand Down
55 changes: 55 additions & 0 deletions examples/partials/partials.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <ministache.hpp>

/***************************************************
This is a very basic example for the ministache library
(https://github.com/floatplane/ministache).
It shows how the library can be used to render a Mustache template with a JSON object.
For more details on Mustache syntax, see http://mustache.github.io/mustache.5.html
****************************************************/

void setup() {
Serial.begin(115200);
Serial.println("");

// Create a JSON object to hold the data that we'll use in our template. It looks like this:
// {
// "Alice": {
// "name": "Alice",
// "role": "Engineer"
// },
// "Bob": {
// "name": "Bob",
// "role": "Intern"
// }
// }
JsonDocument data;
auto people = data["people"].to<JsonArray>();
auto alice = people.add<JsonObject>();
alice["name"] = "Alice";
alice["role"] = "Engineer";
auto bob = people.add<JsonObject>();
bob["name"] = "Bob";
bob["role"] = "Intern";

// Create a template string that renders the data for a single person. This is a partial.
String personString = "Name: {{name}}\tRole: {{role}}\n";

// Create a template string that renders the data for all people. This is the main template.
String reportString = "People report:\n{{#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).
String output = ministache::render(reportString, data, {{"person", personString}});

// Print the result
Serial.println(output);
// People report:
// Name: Alice Role: Engineer
// Name: Bob Role: Intern
}

void loop() {
delay(500);
}

0 comments on commit 1f6a70f

Please sign in to comment.