https://kotlinlang.org logo
#ktor
Title
# ktor
g

Gerard de Leeuw

08/07/2021, 1:09 PM
Copy code
HtmlContent {
        head { title { +"Posts" } }
}
Here you are effectively calling the constructor of HtmlContent and adding content to it, but you are not returning the constructed instance. Your function
respondWithPostList
has no return type, so it will implicitly return
Unit
. I think the most neat solution is to make your
respondWithPostList
function an extension method of
HtmlContent
. So it would look like this:
Copy code
fun HtmlContent.respondWithPostList(posts: List<PostDTO>?) {
        head { title { +"Posts" } }
        body {
            posts?.forEach { post ->
                run {
                    div {
                        h3 {+"${post.title} (${post.id})"}
                        p {+post.author.email}
                    }
                }
            }
        }
}
h

Hadi

08/07/2021, 4:37 PM
Thanks a lot Gerard for the reply. But for the first mistake I made the following should work, shouldn't it?
g

Gerard de Leeuw

08/07/2021, 5:59 PM
Yeah, I think that works too.
👍 1
Theoretically at least, I can't really test it right now 😉
h

Hadi

08/07/2021, 6:16 PM
Yeees, thanks a lot buddy