KTML templates
KTML is a Kotlin-based templating engine for generating HTML. Kilua has built-in support for KTML templates, allowing you to easily use them to render HTML code in your Kilua applications. Using templates can be useful when you have some already defined HTML structures and don't want to rewrite them using the DSL.
Creating KTML templates
To use KTML templates in your Kilua application, you need to include the kilua-ktml module in your project dependencies. You also need apply ktml Gradle plugin.
[versions]
ktml = "0.1.12"
[libraries]
kilua-ktml = { module = "dev.kilua:kilua-ktml", version.ref = "kilua" }
[plugins]
ktml = { id = "dev.ktml.gradle", version.ref = "ktml" }
plugins {
alias(libs.plugins.ktml)
}
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation(libs.kilua.ktml)
}
}
}
}
Check the KTML documentation for more information about KTML templates syntax and features.
Using KTML templates in Kilua
First you need to initialize KTML engine using Ktml.init() method.
class App : Application() {
override fun start() {
Ktml.init(KtmlRegistry)
root("root") {
//
}
}
}
Note
The KtmlRegistry object is generated by the Gradle plugin. You need to run Gradle build process at least once to access this object.
To render a KTML template, use the ktml() composable function. The first argument is the name of the template and the second argument is a map of parameters to be passed to the template.
div {
ktml("card", mutableMapOf("header" to "Template header", "content" to "This is a template card."))
}