Browse Source

Move bindings unwrapping to eval and re-enable caching for includes

master
Shadowfacts 4 years ago
parent
commit
7c17e3e801
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
  1. 17
      src/main/kotlin/net/shadowfacts/ekt/EKT.kt

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

@ -29,7 +29,7 @@ val _env = bindings["_env"] as net.shadowfacts.ekt.EKT.TemplateEnvironment
val _result = StringBuilder()
fun echo(it: Any) { _result.append(it) }
fun include(include: String) {
val env = net.shadowfacts.ekt.EKT.TemplateEnvironment(include, _env, cacheDir = null)
val env = net.shadowfacts.ekt.EKT.TemplateEnvironment(include, _env)
echo(net.shadowfacts.ekt.EKT.render(env, env.include))
}
"""
@ -67,13 +67,7 @@ _result.toString()
}
})
// Hack to allow data to be accessed by name from template instead of via bindings map
val unwrapBindings = env.data.keys.map {
val type = env.data[it]!!.type
"val $it = (bindings[\"$it\"] as net.shadowfacts.ekt.EKT.TypedValue).value as $type"
}.joinToString("\n")
val script = unwrapBindings + scriptPrefix + template + scriptSuffix
val script = scriptPrefix + template + scriptSuffix
if (env.cacheDir != null) {
env.cacheFile.apply {
@ -108,6 +102,13 @@ _result.toString()
bindings.putAll(env.data)
bindings.put("_env", env)
// Hack to allow data to be accessed by name from template instead of via bindings map
val unwrapBindings = env.data.keys.map {
val type = env.data[it]!!.type
"val $it = (bindings[\"$it\"] as net.shadowfacts.ekt.EKT.TypedValue).value as $type"
}.joinToString("\n")
engine.eval(unwrapBindings)
return engine.eval(script) as String
}

Loading…
Cancel
Save