Required dependencies: io.ktor:ktor-server-html-builder
, org.jetbrains.kotlin-wrappers:kotlin-css
CSS DSL extends HTML DSL and allows you to author stylesheets in Kotlin by using the kotlin-css wrapper.
CSS DSL doesn't need installation, but requires including the following artifacts in the build script:
-
The
ktor-server-html-builder
artifact for HTML DSL: -
The
kotlin-css-jvm
artifact for building CSS: You can replace$kotlin_css_version
with the required version of thekotlin-css
artifact, for example,%kotlin_css_version%
.
To send a CSS response, you need to extend ApplicationCall
by adding the respondCss
method to serialize a stylesheet into a string and send it to the client with the CSS
content type:
{src="snippets/caching-headers/src/main/kotlin/com/example/Application.kt" lines="64-66"}
Then, you can provide CSS inside the required route:
{src="snippets/caching-headers/src/main/kotlin/com/example/Application.kt" lines="46-56"}
Finally, you can use the specified CSS for an HTML document created with HTML DSL:
{src="snippets/caching-headers/src/main/kotlin/com/example/Application.kt" lines="34-45"}
You can find the full example here: caching-headers.