Browse Source

Add method for passing objects with generic type params

master
Shadowfacts 5 years ago
parent
commit
86de475391
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
  1. 12
      src/main/kotlin/net/shadowfacts/ekt/EKT.kt
  2. 4
      src/test/kotlin/net/shadowfacts/ekt/Test.kt

12
src/main/kotlin/net/shadowfacts/ekt/EKT.kt

@ -78,12 +78,20 @@ _result.toString()
// Hack to allow data to be accessed by name from template instead of via bindings map
val unwrapBindings = data.keys.map {
val type = data[it]!!::class.qualifiedName
"val $it = bindings[\"$it\"] as $type;"
val value = data[it]!!
if (value is Value) {
val type = value.type
"val $it = (bindings[\"$it\"] as net.shadowfacts.ekt.EKT.Value).value as $type"
} else {
val type = value::class.qualifiedName
"val $it = bindings[\"$it\"] as $type"
}
}.joinToString("\n")
engine.eval(unwrapBindings)
return engine.eval(script)
}
data class Value(val value: Any, val type: String)
}

4
src/test/kotlin/net/shadowfacts/ekt/Test.kt

@ -7,9 +7,7 @@ import java.io.File
*/
fun main(args: Array<String>) {
val res = EKT.render(File("template.ekt"), mapOf(
"title" to "EKT Test",
"body" to "some test content",
"value" to 42
"list" to EKT.Value(listOf(1, 2, 3), "List<String>")
), dumpGeneratedScript = File("script.kts"))
File("result.txt").apply {

Loading…
Cancel
Save