Handlebars.js Templates
Support for Handlebars.js templates is contained in kvision-handlebars module and is based on webpack's handlebars loader. Templates are automatically transformed to JavaScript functions during the build process of the application.
You put your template files (with *.hbs extension) into src/jsMain/resources/modules/hbs directory of your application. Then in your code you can use @JsModule and reference your *.hbs files. All components which render textual content (io.kvision.html.Tag class and subclasses) have template and templates properties. The second one allows you to define different templates for other supported languages (see. Internationalization).
@JsModule("/kotlin/modules/hbs/template.hbs")
external val templateHbs: dynamic
@JsModule("/kotlin/modules/hbs/template.en.hbs")
external val templateEnHbs: dynamic
@JsModule("/kotlin/modules/hbs/template.pl.hbs")
external val templatePlHbs: dynamic
div {
template = templateHbs
}
div {
templates = mapOf(
"en" to templateEnHbs,
"pl" to templatePlHbs
)
}To actually call the template function and render its output in your app, you need to set some data. You can use templateData property and use plain JavaScript object. To generate one from your data you can use obj function builder.
You can also use toObj extension function if you store your data in a class (or classes) with @Serializable annotation.
The setData extension function is a convenient shortcut for the above.
Last updated