Text Templates in Miranda

Java provides built-in support for creating dynamic content or showing customized output to the user with the java.util.StringTemplate class (introduced in Java 21). For earlier versions, you can use the java.text.MessageFormat class or third-party libraries like Apache FreeMarker or Thymeleaf for more advanced templating.

import java.util.StringTemplate;
import java.util.List;
import java.util.Map;

public class TextTemplates {
    public static void main(String[] args) {
        // We can create a new template and parse its body from a string.
        // Templates are a mix of static text and placeholders enclosed in
        // ${...} that are used to dynamically insert content.
        StringTemplate t1 = StringTemplate.of("Value is ${value}\n");

        // By "processing" the template we generate its text with
        // specific values for its placeholders.
        System.out.print(t1.process(Map.of("value", "some text")));
        System.out.print(t1.process(Map.of("value", 5)));
        System.out.print(t1.process(Map.of("value", List.of("Java", "Kotlin", "Scala", "Groovy"))));

        // If the data is a record we can use the ${fieldName} placeholder to access
        // its fields.
        record Person(String name) {}
        StringTemplate t2 = StringTemplate.of("Name: ${name}\n");

        System.out.print(t2.process(new Person("Jane Doe")));

        // The same applies to maps; with maps there is no restriction on the
        // case of key names.
        System.out.print(t2.process(Map.of("name", "Mickey Mouse")));

        // Conditional execution can be achieved using the ternary operator
        // inside the placeholders.
        StringTemplate t3 = StringTemplate.of("${value.isEmpty() ? 'no' : 'yes'}\n");
        System.out.print(t3.process(Map.of("value", "not empty")));
        System.out.print(t3.process(Map.of("value", "")));

        // For looping through collections, we can use the String.join method
        // in combination with StringTemplate.
        StringTemplate t4 = StringTemplate.of("Range: ${String.join(' ', items)}\n");
        System.out.print(t4.process(Map.of("items", List.of("Java", "Kotlin", "Scala", "Groovy"))));
    }
}

To run the program, compile and execute it:

$ javac TextTemplates.java
$ java TextTemplates
Value is some text
Value is 5
Value is [Java, Kotlin, Scala, Groovy]
Name: Jane Doe
Name: Mickey Mouse
yes
no
Range: Java Kotlin Scala Groovy

This example demonstrates how to use Java’s StringTemplate class for basic text templating. It covers string interpolation, working with different data types, conditional expressions, and iterating over collections.

Note that StringTemplate is a simpler templating solution compared to Go’s text/template. For more complex templating needs in Java, you might want to consider using a dedicated templating library like FreeMarker or Thymeleaf, which offer more advanced features such as control structures, custom functions, and template inheritance.