Skip to content

HowTO: Loop Processor

Leon Rosenberg edited this page Oct 14, 2025 · 1 revision

Loop Processor Example and HowTo

Example

You have a template string like this:
{loop:yourCollection:your string, where you replace name - yourCollection.name, and some value - yourCollection.value}

  • loop → the processor name (LoopTemplateProcessor)
  • yourCollection → the name of the collection stored in the template context
  • your string, where you replace name - yourCollection.name, and some value - yourCollection.value → the inner template that should be repeated for every element of the collection

Step 1: Prepare your data

Suppose you have a simple POJO:

public class Item {
    private String name;
    private String value;

    public Item(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() { return name; }
    public String getValue() { return value; }
}

Create a collection of these items:

List<Item> yourCollectionData = List.of(
    new Item("Alice", "123"),
    new Item("Bob", "456"),
    new Item("Charlie", "789")
);

Step 2: Run the processor

String text = "Hello World! {loop:yourCollection:your string, where you replace name - yourCollection.name, and some value - yourCollection.value}";

TemplateReplacementContext context = new TemplateReplacementContext();
context.addAttribute("yourCollection", yourCollectionData); // Put data into the context

String replacedText = TemplateUtility.replaceVariables(context, text);

Step 3: Output

The processor will iterate over the collection and replace placeholders (yourCollection.name, yourCollection.value) with actual field values from each Item.

The output will be (replacedText):

Hello World! your string, where you replace name - Alice, and some value - 123
your string, where you replace name - Bob, and some value - 456
your string, where you replace name - Charlie, and some value - 789

Clone this wiki locally