Text Templates in Squirrel

Java offers several libraries for creating dynamic content or showing customized output to the user. One popular library is Apache FreeMarker, which we’ll use in this example.

First, we need to add the FreeMarker dependency to our project. If you’re using Maven, add this to your pom.xml:

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>

Now, let’s look at the Java code:

import freemarker.template.*;
import java.io.*;
import java.util.*;

public class TemplateExample {
    public static void main(String[] args) throws Exception {
        // Create a configuration instance
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);

        // Create a template from a string
        String templateContent = "Value is ${value}\n";
        Template t1 = new Template("t1", new StringReader(templateContent), cfg);

        // Create a data model
        Map<String, Object> model = new HashMap<>();
        model.put("value", "some text");

        // Process the template
        t1.process(model, new OutputStreamWriter(System.out));

        model.put("value", 5);
        t1.process(model, new OutputStreamWriter(System.out));

        model.put("value", Arrays.asList("Java", "Python", "C++", "C#"));
        t1.process(model, new OutputStreamWriter(System.out));

        // Template for struct-like data
        templateContent = "Name: ${name}\n";
        Template t2 = new Template("t2", new StringReader(templateContent), cfg);

        model.clear();
        model.put("name", "Jane Doe");
        t2.process(model, new OutputStreamWriter(System.out));

        // Template with conditional
        templateContent = "<#if value?has_content>yes<#else>no</#if>\n";
        Template t3 = new Template("t3", new StringReader(templateContent), cfg);

        model.put("value", "not empty");
        t3.process(model, new OutputStreamWriter(System.out));

        model.put("value", "");
        t3.process(model, new OutputStreamWriter(System.out));

        // Template with loop
        templateContent = "Range: <#list values as value>${value} </#list>\n";
        Template t4 = new Template("t4", new StringReader(templateContent), cfg);

        model.put("values", Arrays.asList("Java", "Python", "C++", "C#"));
        t4.process(model, new OutputStreamWriter(System.out));
    }
}

This Java code demonstrates the use of FreeMarker templates, which provide functionality similar to Go’s text templates. Here’s a breakdown of what’s happening:

  1. We create a FreeMarker Configuration object, which is used to set up the template environment.

  2. We create templates from strings using the Template constructor. In FreeMarker, template variables are enclosed in ${...} instead of Go’s {{...}}.

  3. We create a Map to hold the data model, which is used to populate the template.

  4. We process the template using the process method, which takes the data model and an output writer.

  5. We demonstrate conditionals using FreeMarker’s <#if> directive.

  6. We show how to loop through lists using the <#list> directive.

When you run this program, you should see output similar to the following:

Value is some text
Value is 5
Value is [Java, Python, C++, C#]
Name: Jane Doe
yes
no
Range: Java Python C++ C# 

This example shows how to use FreeMarker for text templating in Java, covering basic variable substitution, conditionals, and loops. FreeMarker offers many more features for more complex templating needs.