diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-02-13 13:43:52 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-02-13 13:43:52 +0100 |
commit | 7077c60a88ac90ac05d6b451d4c7a82880667553 (patch) | |
tree | f3e4f72156a2f3f1ab9e0f750693d67188944851 /src/main/kotlin/pl/treksoft/kvision | |
parent | 8beb2bcc4806a69dc99acdd706f7de94bbe35955 (diff) | |
download | kvision-7077c60a88ac90ac05d6b451d4c7a82880667553.tar.gz kvision-7077c60a88ac90ac05d6b451d4c7a82880667553.tar.bz2 kvision-7077c60a88ac90ac05d6b451d4c7a82880667553.zip |
Serializable object support for handlebars.js templates
Diffstat (limited to 'src/main/kotlin/pl/treksoft/kvision')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/html/Template.kt | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/html/Template.kt b/src/main/kotlin/pl/treksoft/kvision/html/Template.kt index 1f377406..7b5b26a5 100644 --- a/src/main/kotlin/pl/treksoft/kvision/html/Template.kt +++ b/src/main/kotlin/pl/treksoft/kvision/html/Template.kt @@ -21,7 +21,11 @@ */ package pl.treksoft.kvision.html +import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.serializer import pl.treksoft.kvision.i18n.I18n +import pl.treksoft.kvision.utils.JSON.toObj /** * Handlebars templates helper interface. @@ -50,3 +54,19 @@ interface Template { content = template?.invoke(value) ?: templates[I18n.language]?.invoke(value) } } + +/** + * Extension function to set serializable object as a template data. + */ +fun <K> Template.setData(obj: K, serializer: SerializationStrategy<K>) { + @Suppress("UnsafeCastFromDynamic") + this.templateData = obj.toObj(serializer) +} + +/** + * Extension function to set serializable object as a template data. + */ +@UseExperimental(ImplicitReflectionSerializer::class) +inline fun <reified K : Any> Template.setData(obj: K) { + this.setData(obj, K::class.serializer()) +} |