-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing error template #43
base: main
Are you sure you want to change the base?
Changes from 4 commits
187d454
4d4ce79
5013ac5
dbe13e0
40a7b48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
import freemarker.cache.ClassTemplateLoader | ||
import freemarker.core.HTMLOutputFormat | ||
import io.ktor.http.* | ||
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 | ||
} | ||
} | ||
} | ||
|
||
suspend fun ApplicationCall.respondTemplates( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the plural in the name here? We must only respond with a single response and this response is rendered based on one template. This template might reference other templates as a form of composition, but it is still a single template. |
||
template: String, // The name of the template to be rendered | ||
model: Any? = null, // The data model to be passed to the template, default is null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have cases where the model is null? |
||
etag: String? = null, // The ETag for caching purposes, default is null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we ever set the etag when calling |
||
contentType: ContentType = ContentType.Text.Html.withCharset(Charsets.UTF_8) // The content type of the response, default is HTML with UTF-8 charset | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice to set the correct content type, but do we need this as a parameter? The templates will always be HTML. If we want a different content format we most likely need to create another mechanism to build our responses. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay as we talked about i just copy/pasted the implementation because it was marked as "Read only" so i didnt thought about it much more. But i will have a look at this and the two things above and see if it breaks something when i delete it or change it |
||
): Unit { | ||
// Retrieve the "HX-Request" header from the request and convert it to a Boolean. Default to false if the header is not present. | ||
val hxRequest = request.header("HX-Request")?.toBoolean() ?: false | ||
|
||
// Prepare the model to include the hxRequest variable. If the original model is a Map, add hxRequest to it. | ||
// Otherwise, create a new map with hxRequest and the original model. | ||
val modelWithHxRequest = if (model is Map<*, *>) { | ||
model + ("hxRequest" to hxRequest) // Add hxRequest to the existing model map | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That could cause problems. In your parameters you accept |
||
mapOf("hxRequest" to hxRequest, "model" to model) // Create a new map containing hxRequest and the original model | ||
} | ||
|
||
// Respond with the rendered FreeMarker template, passing the template name, modified model, etag, and content type. | ||
respond(FreeMarkerContent(template, modelWithHxRequest, etag, contentType)) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
<#global userSession = "null"> | ||
<#global profilePicture = "/static/img/no_profile_picture.png"> | ||
|
||
<#macro base> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Wizard</title> | ||
<link href="/static/style.css" rel="stylesheet" /> | ||
<link rel="stylesheet" | ||
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" | ||
/> | ||
<script src="https://unpkg.com/[email protected]" | ||
integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw" | ||
crossorigin="anonymous"> | ||
</script> | ||
</head> | ||
<body> | ||
<#macro base hxRequest=false> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Wizard</title> | ||
<link href="/static/style.css" rel="stylesheet" /> | ||
<link rel="stylesheet" | ||
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" | ||
/> | ||
<script src="https://unpkg.com/[email protected]" | ||
integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw" | ||
crossorigin="anonymous"> | ||
</script> | ||
</head> | ||
<body> | ||
<#if !hxRequest> | ||
<header> | ||
<nav class="user-menu"> | ||
<!-- Profile Picture Dropdown Section --> | ||
<div class="dropdown tooltip" id="profile-dropdown"> | ||
<a href="#"> | ||
<img class="profile-picture" id="profile-pic" src="${profilePicture}" alt="Profile Picture"> | ||
</a> | ||
<#if userSession == "null"> | ||
<div class="dropdown-content"> | ||
<a href="/signup">Signup</a> | ||
<a href="/login">Login</a> | ||
<nav class="user-menu"> | ||
<!-- Profile Picture Dropdown Section --> | ||
<div class="dropdown tooltip" id="profile-dropdown"> | ||
<a href="#"> | ||
<img class="profile-picture" id="profile-pic" src="${profilePicture}" alt="Profile Picture"> | ||
</a> | ||
<#if userSession == "null"> | ||
<div class="dropdown-content"> | ||
<a href="/signup">Signup</a> | ||
<a href="/login">Login</a> | ||
</div> | ||
<#elseif userSession != "null"> | ||
<div class="tooltip"> | ||
<div class="tooltiptext"> | ||
<#if username??> | ||
<p>Username: ${username} | ||
<#if house??> | ||
<img src="/static/img/${house}_symbol.png" alt="${house} Symbol" style="width: 20px; height: 20px; vertical-align: middle;"> | ||
</#if> | ||
</p> | ||
</#if> | ||
</div> | ||
<!-- End of Profile Picture Dropdown Section --> | ||
<#elseif userSession != "null"> | ||
<div class="tooltip"> | ||
<div class="tooltiptext"> | ||
<#if username??> | ||
<p>Username: ${username} | ||
<#if house??> | ||
<img src="/static/img/${house}_symbol.png" alt="${house} Symbol" style="width: 20px; height: 20px; vertical-align: middle;"> | ||
</#if> | ||
</p> | ||
</#if> | ||
</div> | ||
</div> | ||
<div class="dropdown-content"> | ||
<a href="/profile#favourites">Favourites</a> | ||
<a href="/logout">Logout</a> | ||
</div> | ||
</#if> | ||
</div> | ||
</nav> | ||
</div> | ||
<div class="dropdown-content"> | ||
<a href="/profile#favourites">Favourites</a> | ||
<a href="/logout">Logout</a> | ||
</div> | ||
</#if> | ||
</div> | ||
</nav> | ||
|
||
<h1 class="logo"> | ||
<a href="/">Wizard</a> | ||
|
@@ -63,8 +63,15 @@ | |
</label> | ||
<div class="bg-effect"></div> | ||
</header> | ||
</#if> | ||
|
||
<#if hxRequest> | ||
<section class="container"> | ||
</#if> | ||
|
||
<#nested /> | ||
|
||
<section class="container"> | ||
<#nested> | ||
</section> | ||
</#macro> | ||
<#if hxRequest> | ||
</section> | ||
</#if> | ||
</#macro> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
<h2>${errorMessage}</h2> | ||
|
||
<section class="content"> | ||
<a class="button" href="${redirectLink}">Back</a> | ||
</section> | ||
</@layout.base> | ||
</@layout.base> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a weird import. Usually imports are a reverse FQDN of some kind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
intelij just told me to do this