poi-tl Documentation

repository·master·Indexed 26 days ago

https://github.com/sayi/poi-tl

A logic-less Word template engine based on Apache POI for generating .docx documents from templates and data models. It supports text, picture, table, and numbering tags, as well as conditional rendering, loops, and template nesting. Includes a CLI tool (poi-tl-cli) for generating documents using JSON data models with support for Markdown, code highlighting, and Table of Contents.

Tokens
1.5K
Snippets
6
Records
11
Agent score
40%

What's inside poi-tl

  1. Use Sections for conditional rendering and loops

    master

    Sections are defined by a start tag {{?name}} and an end tag {{/name}}. They behave differently based on the data type:

    1. False/Empty: If the value is null, false, or an empty collection, the content inside the section is hidden.
    2. Non-False/Non-Collection: If the value is a single object (not a collection), the content is rendered once.
    3. Non-Empty Collection: If the value is a collection, the content is looped $N$ times (foreach). Use {{=#this}} inside the loop to refer to the current object.
  2. Install poi-tl via Maven

    master

    Add the following dependency to your pom.xml to use poi-tl. Note that version 1.12.x requires Apache POI version 5.2.2 or higher.

    <dependency>
      <groupId>com.deepoove</groupId>
      <artifactId>poi-tl</artifactId>
      <version>1.12.2</version>
    </dependency>
  3. Quick start with poi-tl

    master

    To generate a document from a template, use the XWPFTemplate.compile() method to load your .docx template, render() it with a data model (e.g., a HashMap), and then writeToFile() to save the output.

    //The core API uses a minimalist design, only one line of code is required
    XWPFTemplate.compile("template.docx").render(new HashMap<String, Object>(){{
             put("title", "poi-tl template engine");
    }}).writeToFile("out_template.docx");
  4. Use Template Nesting

    master

    Nesting allows you to merge one Word template into another using the + prefix, such as {{+nested}}. This is useful for including sub-templates. Use Includes.ofLocal("path.docx").setRenderModel(data).create() to prepare the nested template with its own data model.

    class AddrModel {
      String addr;
      public AddrModel(String addr) {
        this.addr = addr;
      }
    }
    
    List<AddrModel> subData = new ArrayList<>();
    subData.add(new AddrModel("Hangzhou,China"));
    subData.add(new AddrModel("Shanghai,China"));
    put("nested", Includes.ofLocal("sub.docx").setRenderModel(subData).create());
  5. Use Text tags

    master
    The basic text tag {{name}} replaces the tag with the value associated with the key name in your data model. If the key does not exist, the tag is cleared by default (this behavior can be configured). The replacement text inherits the style of the tag in the Word template.
  6. Use Picture tags

    master

    Picture tags start with @. For example, {{@logo}} will replace the tag with an image. The data model value can be a URL string, a local file path, or a structure created via Pictures (e.g., Pictures.ofLocal(...).size(...).create()).

    put("watermelon", "assets/watermelon.png");
    put("watermelon", "http://x/lemon.png");
    put("lemon", Pictures.ofLocal("sob.jpeg", PictureType.JPEG).size(24, 24).create());
  7. Use Table tags

    master

    Table tags start with #. A tag like {{#table}} will be rendered as a Word table with rows and columns determined by the data provided in the model. Use Tables.of(data).create() to prepare the table data.

    put("table", Tables.of(new String[][] {
                    new String[] { "Song name", "Artist" }
                }).border(BorderStyle.DEFAULT).create());
  8. Use Numbering tags

    master

    List tags start with *. A tag like {{*list}} renders as a Word symbol list or numbered list. Use Numberings.create(strings...) to provide the list content.

    put("list", Numberings.create("Plug-in grammar",
                      "Supports word text, pictures, table...",
                      "Template, not just template, but also style template"));
  9. Supported Data Types in poi-tl-cli

    master

    When providing a JSON data model to the CLI, the following specialized render types are supported via the type field in your JSON:

    • markdown: Renders Markdown content using MarkdownRenderData.
    • code: Renders highlighted code blocks using HighlightRenderData.
    • markdown-file: Renders Markdown content from a file using FileMarkdownRenderData.

    Additionally, the CLI includes built-in support for:

    • toc: Table of Contents via TOCRenderPolicy.
    • :: Comments via CommentRenderPolicy.
    • ;: Attachments via AttachmentRenderPolicy.
    • ~: Code highlighting via HighlightRenderPolicy.
    • -: Markdown via MarkdownRenderPolicy.