quarkusquarkus-qute

How define a list in Qute template?


Quarkus version: 3.7.4 I'm trying to define a list of String in a Qute template, something like:

{#let fruits=java.util.Arrays.listOf("Apple", "Orange", "Lime" /}

Is there a way to define a List in Qute template?


Solution

  • This is not possible out of the box, i.e. you can't instantiate a new list in the template.

    However, you could use a custom template extension method like:

    import java.util.List;
    import io.quarkus.qute.TemplateExtension;
    
    @TemplateExtension(namespace = "list")
    public class ListExtensions {
    
       static List<String> of(String... elements) {
          return List.of(elements);
       }
    }
    

    and in the template {#let fruits=list:of("Apple","Orange","Lime")}..use fruits here..{/}.

    You can also access static methods direcly in a template.