diff --git a/src/main/kotlin/net/grandcentrix/backend/plugins/Routing.kt b/src/main/kotlin/net/grandcentrix/backend/plugins/Routing.kt index 8f02e3e..22f9465 100644 --- a/src/main/kotlin/net/grandcentrix/backend/plugins/Routing.kt +++ b/src/main/kotlin/net/grandcentrix/backend/plugins/Routing.kt @@ -18,6 +18,7 @@ import net.grandcentrix.backend.repository.HousesRepository.Companion.HousesRepo import net.grandcentrix.backend.repository.MoviesRepository.Companion.MoviesRepositoryInstance import net.grandcentrix.backend.repository.PotionsRepository.Companion.PotionsRepositoryInstance import net.grandcentrix.backend.repository.SpellsRepository.Companion.SpellsRepositoryInstance +import respondTemplating fun Application.configureRouting() { @@ -114,7 +115,7 @@ fun Application.configureRouting() { get("/books") { val userSession: UserSession? = call.sessions.get() val username = call.sessions.get()?.username - call.respondTemplate( + call.respondTemplating( "books.ftl", mapOf( "books" to BooksRepositoryInstance.getAll(), @@ -129,7 +130,7 @@ fun Application.configureRouting() { get("/houses") { val userSession: UserSession? = call.sessions.get() val username = call.sessions.get()?.username - call.respondTemplate( + call.respondTemplating( "houses.ftl", mapOf( "houses" to HousesRepositoryInstance.getAll(), @@ -144,7 +145,7 @@ fun Application.configureRouting() { get("/characters") { val userSession: UserSession? = call.sessions.get() val username = call.sessions.get()?.username - call.respondTemplate( + call.respondTemplating( "characters.ftl", mapOf( "characters" to CharactersRepositoryInstance.getAll(), @@ -160,7 +161,7 @@ fun Application.configureRouting() { val userSession: UserSession? = call.sessions.get() val username = call.sessions.get()?.username - call.respondTemplate( + call.respondTemplating( "movies.ftl", mapOf( "movies" to MoviesRepositoryInstance.getAll(), @@ -176,7 +177,7 @@ fun Application.configureRouting() { val userSession: UserSession? = call.sessions.get() val username = call.sessions.get()?.username - call.respondTemplate( + call.respondTemplating( "potions.ftl", mapOf( "potions" to PotionsRepositoryInstance.getAll(), @@ -192,7 +193,7 @@ fun Application.configureRouting() { val userSession: UserSession? = call.sessions.get() val username = call.sessions.get()?.username - call.respondTemplate( + call.respondTemplating( "spells.ftl", mapOf( "spells" to SpellsRepositoryInstance.getAll(), diff --git a/src/main/kotlin/net/grandcentrix/backend/plugins/StatusPage.kt b/src/main/kotlin/net/grandcentrix/backend/plugins/StatusPage.kt index 948a8da..30a6952 100644 --- a/src/main/kotlin/net/grandcentrix/backend/plugins/StatusPage.kt +++ b/src/main/kotlin/net/grandcentrix/backend/plugins/StatusPage.kt @@ -10,6 +10,7 @@ import io.ktor.server.routing.* import io.ktor.server.sessions.* import net.grandcentrix.backend.controllers.UserSession import net.grandcentrix.backend.controllers.getProfilePicture +import respondTemplating fun Application.configureStatusPage() { routing { @@ -21,7 +22,7 @@ fun Application.configureStatusPage() { when (cause) { is RequestException -> { val userSession: UserSession? = call.sessions.get() - call.respondTemplate( + call.respondTemplating( "error.ftl", mapOf( "errorMessage" to cause.message, @@ -36,7 +37,7 @@ fun Application.configureStatusPage() { is DAOException -> { val userSession = call.sessions.get() - call.respondTemplate( + call.respondTemplating( "error.ftl", mapOf( "errorMessage" to cause.message, @@ -62,7 +63,7 @@ fun Application.configureStatusPage() { is UserAlreadyExistsException -> { val userSession: UserSession? = call.sessions.get() - call.respondTemplate( + call.respondTemplating( "error.ftl", mapOf( "errorMessage" to cause.message, @@ -79,7 +80,7 @@ fun Application.configureStatusPage() { else -> { val userSession = call.sessions.get() - call.respondTemplate( + call.respondTemplating( "error.ftl", mapOf( "errorMessage" to cause.message, @@ -94,7 +95,7 @@ fun Application.configureStatusPage() { status(HttpStatusCode.NotFound) { call, _ -> val userSession: UserSession? = call.sessions.get() - call.respondTemplate( + call.respondTemplating( "error.ftl", mapOf( "errorMessage" to "Oops! It wasn't possible to find the page, or it doesn't exist.", @@ -107,7 +108,7 @@ fun Application.configureStatusPage() { status(HttpStatusCode.InternalServerError) { call, _ -> val userSession: UserSession? = call.sessions.get() - call.respondTemplate( + call.respondTemplating( "error.ftl", mapOf( "errorMessage" to "Status 500 - Internal Server Error", diff --git a/src/main/kotlin/net/grandcentrix/backend/plugins/Templating.kt b/src/main/kotlin/net/grandcentrix/backend/plugins/Templating.kt index 7f6b670..bc772c6 100644 --- a/src/main/kotlin/net/grandcentrix/backend/plugins/Templating.kt +++ b/src/main/kotlin/net/grandcentrix/backend/plugins/Templating.kt @@ -2,10 +2,28 @@ import freemarker.cache.ClassTemplateLoader import freemarker.core.HTMLOutputFormat import io.ktor.server.application.* import io.ktor.server.freemarker.* +import io.ktor.server.request.* +import io.ktor.server.response.* fun Application.configureTemplating() { install(FreeMarker) { templateLoader = ClassTemplateLoader(this::class.java.classLoader, "templates") outputFormat = HTMLOutputFormat.INSTANCE } -} \ No newline at end of file +} + +suspend fun ApplicationCall.respondTemplating( + template: String, + model: Map<*, *>? = null +): Unit { + val hxRequest = request.header("HX-Request")?.toBoolean() ?: false + + val modelWithHxRequest: Map<*, *> = when (model) { + is Map<*, *> -> model + ("hxRequest" to hxRequest) + null -> mapOf("hxRequest" to hxRequest) + else -> throw IllegalArgumentException("Model must be a Map") + } + + respond(FreeMarkerContent(template, modelWithHxRequest)) +} + diff --git a/src/main/resources/templates/_layout.ftl b/src/main/resources/templates/_layout.ftl index 76f0653..4be91b9 100644 --- a/src/main/resources/templates/_layout.ftl +++ b/src/main/resources/templates/_layout.ftl @@ -1,53 +1,53 @@ <#global userSession = "null"> <#global profilePicture = "/static/img/no_profile_picture.png"> -<#macro base> - - - - Wizard - - - - - +<#macro base hxRequest=false> + + + + Wizard + + + + + +<#if !hxRequest>
-

Wizard @@ -63,8 +63,15 @@

+ + +<#if hxRequest> +
+ + + <#nested /> -
- <#nested> -
- \ No newline at end of file + <#if hxRequest> +
+ + diff --git a/src/main/resources/templates/error.ftl b/src/main/resources/templates/error.ftl index c2ecbf1..ab9723f 100644 --- a/src/main/resources/templates/error.ftl +++ b/src/main/resources/templates/error.ftl @@ -1,12 +1,9 @@ <#import "_layout.ftl" as layout /> -<#assign userSession = session in layout> -<#assign profilePicture = profilePictureData in layout> - -<@layout.base> +<@layout.base hxRequest=hxRequest>

${errorMessage}

Back
- \ No newline at end of file +