Allow passing data to includes, falling back to current data

This commit is contained in:
Shadowfacts 2017-08-22 17:41:40 -04:00
parent b8d4debfb7
commit dc40cc2647
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 22 additions and 15 deletions

View File

@ -30,10 +30,13 @@ object EKT {
val _env = bindings["_env"] as net.shadowfacts.ekt.EKT.TemplateEnvironment 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, data: Map<String, net.shadowfacts.ekt.EKT.TypedValue>? = null) {
val env = _env.createChild(include) val env = _env.createChild(include, data)
echo(net.shadowfacts.ekt.EKT.render(env, env.include)) 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 = """ private val scriptSuffix = """
_result.toString() _result.toString()
@ -145,7 +148,7 @@ _result.toString()
val cacheFile: File val cacheFile: File
get() = File(cacheDir!!, "$name.kts") get() = File(cacheDir!!, "$name.kts")
fun createChild(name: String): TemplateEnvironment fun createChild(name: String, data: Map<String, TypedValue>? = null): TemplateEnvironment
} }
@ -175,17 +178,17 @@ _result.toString()
constructor(name: String, templateDir: File, includeDir: File, cacheDir: File?, init: DataProvider.() -> Unit): constructor(name: String, templateDir: File, includeDir: File, cacheDir: File?, init: DataProvider.() -> Unit):
this(name, templateDir, includeDir, cacheDir, DataProvider.init(init)) this(name, templateDir, includeDir, cacheDir, DataProvider.init(init))
constructor(name: String, parent: FileTemplateEnvironment, cacheDir: File? = parent.cacheDir) { constructor(name: String, parent: FileTemplateEnvironment, data: Map<String, TypedValue>?) {
this.rootName = parent.rootName this.rootName = parent.rootName
this.name = name this.name = name
this.templateDir = parent.templateDir this.templateDir = parent.templateDir
this.includeDir = parent.includeDir this.includeDir = parent.includeDir
this.cacheDir = cacheDir this.cacheDir = parent.cacheDir
this.data = parent.data this.data = data ?: parent.data
} }
override fun createChild(name: String): TemplateEnvironment { override fun createChild(name: String, data: Map<String, TypedValue>?): TemplateEnvironment {
return FileTemplateEnvironment(name, this) return FileTemplateEnvironment(name, this, data)
} }
} }
@ -216,17 +219,17 @@ _result.toString()
constructor(name: String, templatePath: String, includePath: String, cacheDir: File?, init: DataProvider.() -> Unit): constructor(name: String, templatePath: String, includePath: String, cacheDir: File?, init: DataProvider.() -> Unit):
this(name, templatePath, includePath, cacheDir, DataProvider.init(init)) this(name, templatePath, includePath, cacheDir, DataProvider.init(init))
constructor(name: String, parent: ClasspathTemplateEnvironment, cacheDir: File? = parent.cacheDir) { constructor(name: String, parent: ClasspathTemplateEnvironment, data: Map<String, TypedValue>?) {
this.rootName = parent.rootName this.rootName = parent.rootName
this.name = name this.name = name
this.templatePath = parent.templatePath this.templatePath = parent.templatePath
this.includePath = parent.includePath this.includePath = parent.includePath
this.cacheDir = cacheDir this.cacheDir = parent.cacheDir
this.data = parent.data this.data = data ?: parent.data
} }
override fun createChild(name: String): TemplateEnvironment { override fun createChild(name: String, data: Map<String, TypedValue>?): TemplateEnvironment {
return ClasspathTemplateEnvironment(name, this) return ClasspathTemplateEnvironment(name, this, data)
} }
} }

View File

@ -1,5 +1,5 @@
--------- ---------
Classpath include Classpath include
[= list =] foo: [= foo =]
My name is: [= _env.name =] My name is: [= _env.name =]
Root name: [= _env.rootName =] Root name: [= _env.rootName =]

View File

@ -8,4 +8,8 @@ Environment type: [= _env::class.simpleName =]
Sum: [= list.sum() =] Sum: [= list.sum() =]
[: include("include") :] [:
include("include") {
"foo" to "bar"
}
:]