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

This commit is contained in:
Shadowfacts 2017-08-07 17:22:43 -04:00
parent 750ab89482
commit 7c17e3e801
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 9 additions and 8 deletions

View File

@ -29,7 +29,7 @@ val _env = bindings["_env"] as net.shadowfacts.ekt.EKT.TemplateEnvironment
val _result = StringBuilder() val _result = StringBuilder()
fun echo(it: Any) { _result.append(it) } fun echo(it: Any) { _result.append(it) }
fun include(include: String) { 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)) 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 script = scriptPrefix + template + scriptSuffix
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
if (env.cacheDir != null) { if (env.cacheDir != null) {
env.cacheFile.apply { env.cacheFile.apply {
@ -108,6 +102,13 @@ _result.toString()
bindings.putAll(env.data) bindings.putAll(env.data)
bindings.put("_env", env) 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 return engine.eval(script) as String
} }