diff --git a/README.md b/README.md index c6fd588..40f07cd 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Feature complete. Just some test coverage to complete and addition of Javadoc. * No dependencies, JPMS compliant, Graal Native Image friendly * Fast. See Design Choices. * Simple Java. Public API consists of just 2 main classes, `TemplateModel` and `TemplateProcessor`. - * Simple Content. Just `` (and ``), ``, `` and ``. Bash like variable such as `${myVar}`. + * Simple Content. Just `` (and ``), ``, `` and ``. Bash like variable such as `${myVar}`. * Internationalisation features. ## Design Choices @@ -90,11 +90,11 @@ public class Example1 {
    - +

    Name: ${name}

    Age: ${age}

    Location: ${location}

    -
    + """). diff --git a/src/main/java/com/sshtools/tinytemplate/Templates.java b/src/main/java/com/sshtools/tinytemplate/Templates.java index 2d6f658..f8b3da9 100644 --- a/src/main/java/com/sshtools/tinytemplate/Templates.java +++ b/src/main/java/com/sshtools/tinytemplate/Templates.java @@ -849,7 +849,7 @@ else if (ch == 't') { case T_TAG_NAME: if (ch == '>') { var directive = buf.toString().substring(1).trim(); - if(directive.startsWith("t:if ") || directive.startsWith("t:list ")) { + if(directive.startsWith("t:if ") || directive.startsWith("t:list ") || directive.startsWith("t:object ")) { block.nestDepth++; } if(process) { @@ -999,14 +999,14 @@ else if(dir.equals("t:else")) { block.state = State.START; return true; } - else if(dir.equals("t:template")) { + else if(dir.equals("t:object")) { var templateSupplier = block.model.templates.get(var); if (templateSupplier == null) { logger.ifPresent(l -> l.warning("Missing template {0} in message template", var)); return false; } else { - var templBlock = new Block(block.model, block.expander, block.reader, "template", true); + var templBlock = new Block(block.model, block.expander, block.reader, "object", true); templBlock.nestDepth = 1; templBlock.capture = true; read(templBlock); diff --git a/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java b/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java index dc4f8d7..12f6a64 100644 --- a/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java +++ b/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java @@ -893,6 +893,66 @@ public void testTemplateNestedIf3() { } + @Test + public void testTemplateObjectMissing() { + Assertions.assertEquals(""" + + + + + + """, + createParser().process(TemplateModel.ofContent(""" + + + +

    Should not be visible

    +

    Name: ${name}

    +
    + + + """). + variable("name", "Joe B"))); + + } + + @Test + public void testTemplateObject() { + Assertions.assertEquals(""" + + +

    Some text

    + +

    Name: Joe B

    +

    Age: 27

    +

    Location: London

    + +

    Some other text

    + + + """, + createParser().process(TemplateModel.ofContent(""" + + +

    Some text

    + +

    Name: ${name}

    +

    Age: ${age}

    +

    Location: ${location}

    +
    +

    Some other text

    + + + """). + template("aPerson", (c) -> + TemplateModel.ofContent(c). + variable("name", "Joe B"). + variable("age", "27"). + variable("location", "London") + ))); + + } + private TemplateProcessor createParser() { return new TemplateProcessor.Builder().build(); }