From 7077c60a88ac90ac05d6b451d4c7a82880667553 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Wed, 13 Feb 2019 13:43:52 +0100 Subject: Serializable object support for handlebars.js templates --- src/main/kotlin/pl/treksoft/kvision/html/Template.kt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/main') 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 Template.setData(obj: K, serializer: SerializationStrategy) { + @Suppress("UnsafeCastFromDynamic") + this.templateData = obj.toObj(serializer) +} + +/** + * Extension function to set serializable object as a template data. + */ +@UseExperimental(ImplicitReflectionSerializer::class) +inline fun Template.setData(obj: K) { + this.setData(obj, K::class.serializer()) +} -- cgit