From dc40cc26472d4b3d620ed5e553849150fa72aaa1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 22 Aug 2017 17:41:40 -0400 Subject: [PATCH] Allow passing data to includes, falling back to current data --- src/main/kotlin/net/shadowfacts/ekt/EKT.kt | 29 ++++++++++--------- .../resources/templates/includes/include.ekt | 2 +- src/test/resources/templates/template.ekt | 6 +++- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/ekt/EKT.kt b/src/main/kotlin/net/shadowfacts/ekt/EKT.kt index 91cc022..c68776b 100644 --- a/src/main/kotlin/net/shadowfacts/ekt/EKT.kt +++ b/src/main/kotlin/net/shadowfacts/ekt/EKT.kt @@ -30,10 +30,13 @@ object EKT { 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 = _env.createChild(include) +fun include(include: String, data: Map? = null) { + val env = _env.createChild(include, data) echo(net.shadowfacts.ekt.EKT.render(env, env.include)) } +fun include(include: String, init: net.shadowfacts.ekt.EKT.DataProvider.() -> Unit) { + include(include, data = net.shadowfacts.ekt.EKT.DataProvider.init(init)) +} """ private val scriptSuffix = """ _result.toString() @@ -145,7 +148,7 @@ _result.toString() val cacheFile: File get() = File(cacheDir!!, "$name.kts") - fun createChild(name: String): TemplateEnvironment + fun createChild(name: String, data: Map? = null): TemplateEnvironment } @@ -175,17 +178,17 @@ _result.toString() constructor(name: String, templateDir: File, includeDir: File, cacheDir: File?, init: DataProvider.() -> Unit): this(name, templateDir, includeDir, cacheDir, DataProvider.init(init)) - constructor(name: String, parent: FileTemplateEnvironment, cacheDir: File? = parent.cacheDir) { + constructor(name: String, parent: FileTemplateEnvironment, data: Map?) { this.rootName = parent.rootName this.name = name this.templateDir = parent.templateDir this.includeDir = parent.includeDir - this.cacheDir = cacheDir - this.data = parent.data + this.cacheDir = parent.cacheDir + this.data = data ?: parent.data } - override fun createChild(name: String): TemplateEnvironment { - return FileTemplateEnvironment(name, this) + override fun createChild(name: String, data: Map?): TemplateEnvironment { + return FileTemplateEnvironment(name, this, data) } } @@ -216,17 +219,17 @@ _result.toString() constructor(name: String, templatePath: String, includePath: String, cacheDir: File?, init: DataProvider.() -> Unit): this(name, templatePath, includePath, cacheDir, DataProvider.init(init)) - constructor(name: String, parent: ClasspathTemplateEnvironment, cacheDir: File? = parent.cacheDir) { + constructor(name: String, parent: ClasspathTemplateEnvironment, data: Map?) { this.rootName = parent.rootName this.name = name this.templatePath = parent.templatePath this.includePath = parent.includePath - this.cacheDir = cacheDir - this.data = parent.data + this.cacheDir = parent.cacheDir + this.data = data ?: parent.data } - override fun createChild(name: String): TemplateEnvironment { - return ClasspathTemplateEnvironment(name, this) + override fun createChild(name: String, data: Map?): TemplateEnvironment { + return ClasspathTemplateEnvironment(name, this, data) } } diff --git a/src/test/resources/templates/includes/include.ekt b/src/test/resources/templates/includes/include.ekt index a26e0d1..087eec3 100644 --- a/src/test/resources/templates/includes/include.ekt +++ b/src/test/resources/templates/includes/include.ekt @@ -1,5 +1,5 @@ --------- Classpath include -[= list =] +foo: [= foo =] My name is: [= _env.name =] Root name: [= _env.rootName =] \ No newline at end of file diff --git a/src/test/resources/templates/template.ekt b/src/test/resources/templates/template.ekt index b00e1f7..7cda184 100644 --- a/src/test/resources/templates/template.ekt +++ b/src/test/resources/templates/template.ekt @@ -8,4 +8,8 @@ Environment type: [= _env::class.simpleName =] Sum: [= list.sum() =] -[: include("include") :] \ No newline at end of file +[: +include("include") { + "foo" to "bar" +} +:] \ No newline at end of file