Search code examples
stringkotlinescapingrenderinghtml-escape

How to unescape html (in String) in Koreander templates?


I am using lukasjapan/koreander library along with its ktor-support version, and when I call ViewModel(Res class) topbar variable from in kor file, it escapes the html String.

This is how I'm calling ktor to serve web-pages:

suspend fun main() = coroutineScope<Unit> {
    System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE")
    embeddedServer(
        Netty,
        port = 80,
        module = Application::module
    ).apply { start(wait = true) }
}

data class Res(val topbar: String)

fun Application.module() {
    install(KoreanderFeature)
    routing {
        get("/") {
            val text = javaClass.getResource("/templates/topbar.kor").readText()
            val resource = Res(Koreander().render(text, Any()))
            call.respondKorRes("/templates/index.kor", resource)
        }
    }
}

This is my topbar.kor file: This is just for testing so i make it just small.

.navbar-custom
    %ul.list-unstyled.topnav-menu.float-right.mb-0
        %li.d-none.d-sm-block
            %form.app-search

This is my index.kor file:

%html
    %head
    %body
        %p.hello Hello World!
        $topbar / <- problem here, this has escaped html, browser interpret it as text

This is what rendered on the browser: Browser-snapshot How do I unescape the html here so that browser interpret it as a valid html and render it on there?


Solution

  • I got answer from David Eriksson @ official kotlinlang-slack

    We can use :unsafehtml filter to effectively bypass HTML escaping:

    %html
        %head
        %body
            %p.hello Hello World!
            :unsafehtml $topbar