Martin Gaens
04/05/2022, 11:24 PMrouting {
get("/") {
call.respond(
FreeMarkerContent(
"index.ftl",
mapOf(
"apartments" to transaction { Apartment.all().toList() }
)
)
)
}
}
The transaction produces a List
of `Apartment`s. Now, the problem is, that the Apartment
object holds a property called Address
but this property is from a different table. So, in order to access the `Address`' name, we have to call address.name
. This is the FreeMarker code:
<ol>
<#list apartments as apartment>
<li>${apartment.address.name} ${apartment.apartmentNumber} ${apartment.room}</li>
</#list>
</ol>
And this is the error it produces:
freemarker.core._TemplateModelException: An error has occurred when reading existing sub-variable "address"; see cause exception! The type of the containing value was: extended_hash+string (com.gitlab.djsushi.data.table.Apartment wrapped into f.e.b.StringModel)
Ron S
04/06/2022, 9:33 AMRon S
04/06/2022, 9:38 AMMartin Gaens
04/07/2022, 12:51 PMwith()
option doesn't work, so I'll have to go with the other option, unfortunately. But I feel like it's kind of dirty and repetitive. Is there any common practice for this kind of thing?Ron S
04/07/2022, 1:02 PMRon S
04/07/2022, 1:03 PMMartin Gaens
04/07/2022, 1:08 PMMartin Gaens
04/07/2022, 9:15 PMget("/") {
call.respond(
ThymeleafContent(
template = "addresses",
model = mapOf(
"apartments" to transaction {
Apartment.all().map {
mapOf(
"address" to it.address.name,
"apartmentNumber" to it.apartmentNumber,
"room" to it.room
)
}
},
"addresses" to transaction {
Address.all().toList().map { it.name }
}
)
)
)
}
Martin Gaens
04/07/2022, 9:17 PMEntity
classes or anything like that, just make a Map
and put all the stuff you need into it and pass it to Thymeleaf. The only disadvantage is IDE autocompletion and syntax highlighting. Now, IntelliJ is unable to do it properly, but it wasn't as good even before that. And yeah I switched to Thymeleaf from FreeMarker, because it seemed like the better option. Although FreeMarker has better IDE support from intelliJ in my opinion.Ron S
04/10/2022, 8:50 AM