Skip to content

Commit

Permalink
example: added multipart document example
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed May 21, 2019
1 parent 693be3d commit 1daac6e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/Multipart Template/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import io.github.erdos.stencil.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
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"));

// prepare header fragment
PreparedFragment header = API.fragment(findFile("header.docx"));
PreparedFragment footer = API.fragment(findFile("footer.docx"));

Map<String, PreparedFragment> fragments = new HashMap<>();
fragments.put("header", header);
fragments.put("footer", footer);

// assemble template data
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("companyName", "ACME Company");
dataMap.put("companyAddress", "3786 Farm Meadow Drive, Nashville TN, 37214");

TemplateData data = TemplateData.fromMap(dataMap);

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

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

private static File findFile(String name) {
return new File(Main.class.getResource(name).getFile());
}
}
10 changes: 10 additions & 0 deletions examples/Multipart Template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Multipart document example

In this example, the contents of the header and footer are stored in distinct template files. These templates
may also refer dynamic content from the template data map.

Using multipart templates are useful in many scenarios. For example, you can store addresses, company information
or legal texts in separate documents. You can also reuse common elements across templates to make it easier to change
them in the future.

See the [Main.java](Main.java) file for usage.
Binary file added examples/Multipart Template/footer.docx
Binary file not shown.
Binary file added examples/Multipart Template/header.docx
Binary file not shown.
Binary file added examples/Multipart Template/template.docx
Binary file not shown.

0 comments on commit 1daac6e

Please sign in to comment.