diff --git a/README.md b/README.md index c9c8602..ca9755e 100644 --- a/README.md +++ b/README.md @@ -364,7 +364,66 @@ if(me != null) { #### Include -TODO +Includes allows templates to be nested. When a `` tag is encountered in the template, a corresponding `my_include` is looked up in the current `TemplateModel`. This include itself, is a new `TemplateModel`, with it's own content (derived from a `String`, a resource, `Path` or whatever) as any other template. + +The include tag would be a key tool if you were to use TinyTemplate to compose pages of lots of smaller parts. + +*An include must be completely self contained. It no direct access to the template it is contained within. Like any other template, all variables (and potentially further nested includes) must be provided specifically to it.* + +**main.html** + +```html + + + +

My Main Content

+ + +``` + +**menu.frag.html** + +```html + + +
    + +
  • ${action}
  • + +
      + + +``` + +*Note, the use of `` and ``. This is not strictly required, it is to help your IDE cope with fragments of HTML with custom tags. See above. * + +**Main.java** + +```java + + +public record Anchor(String href, String text) {} + +// ... + +var links = Set.of( + new Anchor("file.html", "File"), + new Anchor("edit.html", "Edit"), + new Anchor("view.html", "View"), + new Anchor("help.html", "Help") +); + +var model = TemplateModel.ofResource(Main.class, "main.html"). + model.object("nav_menu", content -> + TemplateModel.ofResource(Main.class, "menu.frag.html"). + list("menu", (content) -> + links.stream().map(anchor -> TemplateModel.ofContent(content). + variable("href", anchor::href). + variable("text", anchor::text) + ).toList() + ) + ); +``` #### List