Templating engines

Example showing how to use the Templating modules - in this case Handlebars, both by standard response manipulation and via a typesafe view lens.

Gradle setup

implementation group: "org.http4k", name: "http4k-core", version: "3.270.0"
implementation group: "org.http4k", name: "http4k-template-handlebars", version: "3.270.0"


package cookbook.using_templates

import org.http4k.core.Body
import org.http4k.core.ContentType.Companion.TEXT_HTML
import org.http4k.core.HttpHandler
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.with
import org.http4k.template.HandlebarsTemplates
import org.http4k.template.ViewModel
import org.http4k.template.viewModel

// this view has the default template path of: cookbook/using_templates/Person.hbs, although that is overridable by
// setting the template property from ViewModel
data class Person(val name: String, val age: Int) : ViewModel

fun main() {

    val renderer = HandlebarsTemplates().HotReload("src/docs")

    val view = Body.viewModel(renderer, TEXT_HTML).toLens()

    val app: HttpHandler = {
        val viewModel = Person("Bob", 45)
        // OR:
        Response(OK).with(view of viewModel)

    println(app(Request(GET, "/someUrl")))