Kest

Search

Search the documentation

HTTP

Views (MVC)

Server-rendered pages with @Render and a template engine: Mustache built in, FreeMarker and Thymeleaf as add-ons.

NestJS has @Render('index') and template engines such as Handlebars. Kest has the same, with three engines.

Set up

// build.gradle.kts
implementation("io.kest.framework:views")
 
Kest.run<AppModule> {
    import(ViewsModule.forRoot {
        layout = "layout"                                  // wraps every page; sees {{{body}}}
        globals = mapOf("site" to "Kest Shop")             // available in every template
    })
}

Templates live on the classpath at templates/<name>.mustache (src/main/resources/templates/). While developing, directory("src/main/resources/templates") reads them from disk on every request, so edits show without restarting.

Render

@Controller("/shop")
class ShopPages(private val products: ProductService) {
    @Get("/") @Render("home")
    fun home() = mapOf("title" to "Shop", "items" to products.all())          // the return value is the model
 
    @Get("/{id}") fun product(@Param id: String): View =
        products.find(id)?.let { View("product", it) } ?: View("not-found", status = 404)
}

The model may be a map or any object; an object's getters or Kotlin properties become the template variables.

<!-- templates/home.mustache -->
<h1>{{title}}</h1>
{{#items}}<li>{{name}}: {{price}}</li>{{/items}}
{{^items}}<p>nothing yet</p>{{/items}}
{{> footer}}
<!-- templates/layout.mustache -->
<html><head><title>{{site}} - {{title}}</title></head><body>{{{body}}}</body></html>

Mustache

{{name}} is HTML-escaped, {{{name}}} and {{&name}} are raw, {{a.b.c}} follows paths, {{.}} is the current item. {{#x}}...{{/x}} repeats a list or shows once for true or an object; {{^x}}...{{/x}} shows when empty or false. {{! comment }}, {{> partial}} and {{=<% %>=}} work too. A missing template, an unclosed section or an unknown partial throws a TemplateException, which is a 500, never a blank page.

FreeMarker and Thymeleaf

implementation("io.kest.framework:views-freemarker")      // templates/*.ftl
import(ViewsModule.forRoot { engine = FreeMarkerEngine("templates") })
 
implementation("io.kest.framework:views-thymeleaf")       // templates/*.html with th:text, th:each...
import(ViewsModule.forRoot { engine = ThymeleafEngine("templates") })

Any ViewEngine can be plugged in: it has render(name, model) and optionally renderString(source, model). The same engines render mail templates.

Not built

There are no static-asset conventions beyond serveStatic, no view helpers or i18n, and no live reload.