Skip to content

Commit

Permalink
example: purchase reminder has readme and example java code
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed May 21, 2019
1 parent 893644c commit 3d97a59
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
37 changes: 37 additions & 0 deletions examples/Purchase Reminder/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import io.github.erdos.stencil.API;
import io.github.erdos.stencil.EvaluatedDocument;
import io.github.erdos.stencil.PreparedTemplate;
import io.github.erdos.stencil.TemplateData;
import io.github.erdos.stencil.standalone.JsonParser;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;

public class Main {

public static void main(String... args) throws IOException {

// prepare template file
PreparedTemplate template = API.prepare(findFile("template.docx"));

// read template data from a JSON
String jsonData = new String(Files.readAllBytes(findFile("data.json").toPath()));
Map templateDataMap = (Map) JsonParser.parse(jsonData);

// assemble template data
TemplateData data = TemplateData.fromMap(templateDataMap);

// render template
EvaluatedDocument result = API.render(template, data);

// write generated document to a file
File output = new File("purchase-reminder-output.docx");
result.writeToFile(output);
}

private static File findFile(String name) {
return new File(Main.class.getResource(name).getFile());
}
}
5 changes: 5 additions & 0 deletions examples/Purchase Reminder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Simple variables and tables

In this example, you can see how to insert simple variables in a template. You can also see a table with dynamic rows.

See the [Main.java](Main.java) file for usage.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* A naive implementation based on recursive descent parsing.
*/
final class JsonParser {
public final class JsonParser {

/**
* Parses string and returns read object if any.
Expand Down

0 comments on commit 3d97a59

Please sign in to comment.