Skip to main content

Views

See the Views section for an overview.

InvirtView

Base class for view models that associate a Pebble template with the rendering object.

data class ListUsersResponse(
val users: List<User>
) : InvirtView("users/list")

InvirtView.ok / InvirtView.status

Renders an InvirtView with status 200 or a caller-supplied status.

"/users" GET { request ->
ListUsersResponse(users).ok(request)
}

"/users" POST { request ->
CreateUserResponse(user).status(request, Status.ACCEPTED)
}

renderTemplate

Renders a template directly, optionally with a model object, without a backing InvirtView.

"/" GET { request -> renderTemplate(request, "index") }
"/profile" GET { request -> renderTemplate(request, "profile", mapOf("name" to "John")) }

errorResponse

Renders a template with validation errors and an optional model. Returns Status.UNPROCESSABLE_ENTITY by default.

error { form, errors ->
errorResponse(request, errors, "signup", form)
}

// One-off field errors without a backing model
errorResponse(request, "signup", "email" to "Email already in use")

InvirtView.asErrorResponse

Convenience that treats an InvirtView instance as both the model and the template name when rendering an error response.

fun InvirtView.asErrorResponse(
request: Request,
errors: ValidationErrors,
status: Status = Status.UNPROCESSABLE_ENTITY
): Response