Hadi
08/07/2021, 8:29 AMroute("{year}") {
get {
val year = call.parameters["year"]!!.toIntOrNull()
if (year == null) {
call.respondText { "Year can only be an integer" }
} else {
val posts = PostRepo.getByDate(year)
call.respondHtml { respondWithPostList(posts) }
}
}
}
fun respondWithPostList(posts: List<PostDTO>?) {
HtmlContent {
head { title { +"Posts" } }
body {
posts?.forEach { post ->
run {
div {
h3 {+"${post.title} (${post.id})"}
p {+post.author.email}
}
}
}
}
}
}
But it does not work. However, the following works:
route("{year}") {
get {
val year = call.parameters["year"]!!.toIntOrNull()
if (year == null) {
call.respondText { "Year can only be an integer" }
} else {
val posts = PostRepo.getByDate(year)
call.respondHtml {
head { title { +"Posts" } }
body {
posts?.forEach { post ->
run {
div {
h3 {
+"${post.title} (${post.id})"
}
p {
+post.author.email
}
}
}
}
}
}
}
}
}
Does anyone know what I do wrong? what is the correct way to produce HTML in a function?Aleksei Tirman [JB]
08/09/2021, 9:50 AMHtmlContent
returns OutgoingContent
you can return it from respondWithPostList
function and use the result by `call.respond`:
// ...
call.respond(respondWithPostList(posts))
// ...
fun respondWithPostList(posts: List<PostDTO>?): OutgoingContent {
return HtmlContent {
// ...
}
}