ultraq / thymeleaf-layout-dialect

A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse
https://ultraq.github.io/thymeleaf-layout-dialect/
Apache License 2.0
700 stars 111 forks source link

Does layout dialect work with TemplateMode.TEXT? #226

Open GGuedesAB opened 1 year ago

GGuedesAB commented 1 year ago

When I request a template to be processed, it does not decorate the template it should decorate. Am I missing something? I read https://ultraq.github.io/thymeleaf-layout-dialect/ and https://www.thymeleaf.org/doc/articles/layouts.html and could not find any information about the support of TemplateMode.TEXT.

Here is my testcase:

Thymeleaf v3.0.15 thymeleaf-layout-dialect v3.1.0 JDK v1.8.0_65:

layout.txt:

List of contents:
[# layout:fragment="list" /]

contents.txt:

[# layout:decorate="~{layout}"]
[# layout:fragment="list"]
[# th:each="element: ${myList}"][[${element}]], [/]
[/]
[/]

TemplateGen.java:

public class TemplateGen {
    public static void main(String[] args) {
        FileTemplateResolver templateResolver = new FileTemplateResolver();
        templateResolver.setTemplateMode(TemplateMode.TEXT);
        templateResolver.setPrefix("templates/");
        templateResolver.setSuffix(".txt");
        final TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        templateEngine.addDialect(new LayoutDialect(null, false));

        List<String> myList = Arrays.asList("Pepper", "Milk", "Tea");
        final Context ctx = new Context();
        ctx.setVariable("myList", myList);
        Writer output = new OutputStreamWriter(System.out);
        templateEngine.process("contents", ctx, output);
    }
}

Output:


Pepper, Milk, Tea, 
ultraq commented 1 year ago

Unfortunately the layout dialect does not support the TEXT template mode, only HTML and XML. It could be something to add - I think you're the first to ever ask about it!

JapuDCret commented 4 months ago

Would also be interested in this