How do I make `http4k-template-thymeleaf` look for...
# http4k
m
How do I make
http4k-template-thymeleaf
look for templates with
.html
suffix? I am trying this:
Copy code
data class Person(val name: String, val age: Int) : ViewModel

fun main() {
    val renderer = ThymeleafTemplates().CachingClasspath("templates")

    val app: HttpHandler = {
        val viewModel = Person("Bob", 45)
        Response(OK).body(renderer(viewModel))
    }

    app.asServer(SunHttp(8000)).start()
}
but it expects the template file to be
Person
(without suffix), I want it to be
Person.html
.
m
Ah, nice!
Seems like I cannot use Maven `exec:java` with http4k+Thymeleaf, since it uses `ClassLoader.getSystemClassLoader()`: https://stackoverflow.com/questions/66375562/loading-resources-in-a-maven-project-with-exec-maven-plugin 😕
s
I'm aware of another example by @gypsydave5, but using handlebars: https://github.com/gypsydave5/todo-kotlin-htmx/tree/main
g
ahahahah maybe I should tidy that up 😳
s
@gypsydave5 Why?! it's the greatest already 😛
g
Needs the webdriver and maybe not using strikt...
I'll make a todo list
😂 1
m
Seems like the Thymeleaf integration does not really work as expected. According to the Thymeleaf documentation, you are supposed to be able to put fragments in
footer.html
, and refer to them with
Copy code
th:insert="~{footer :: copy}"
but, I need to do
Copy code
th:insert="~{footer.html :: copy}"
or rename the file to
footer
(without suffix). So it seems like Thymeleaf expect the integrating framework to add a
.html
suffix automagically, which http4k does not.
d
That seems like a failing of the actual lib because it should handle more than just HTML files (unless it magically takes the same suffix name as the current template). Can you configure thymeleaf at all to do this for you? (When constructing the engine)
m
Not sure, I have not used Thymeleaf before.
But it seems like the Spring Boot integration does this.
And according to Thymeleaf docs, you are supposed to `setSuffix`: https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html#the-template-resolver
Here I am specifying that the content type is HTML:
Copy code
val renderer = ThymeleafTemplates().CachingClasspath("templates")
    val htmlLens = Body.viewModel(renderer, TEXT_HTML).toLens()
It would make sense if this also set
.html
suffix on the templates (which seems to be the convention for Thymeleaf), and maybe also
templateResolver.setTemplateMode(TemplateMode.HTML)
.
If I also want to use templates for other formats than HTML which Thymeleaf supports, e.g. CSS, then I think it would make sense to have another lens with another renderer for that.
PR to use
.html
suffix by default for Thymeleaf templates: https://github.com/http4k/http4k/pull/942