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