Repetitive template returning #102
-
I found myself doing this quite often: @GetMapping("/view/users")
fun viewAllUsers(model: Model, @RequestHeader(HX_REQUEST) hxRequest: Boolean = false): String {
transaction {
model.addAttribute("users", UserEntity.all().toList())
}
return if (hxRequest) "users/list :: content" else "users/list"
} The bit that annoys me is the return part. Returning a partial if coming from htmx, or a full view if its full reload. Any ideas of improvement? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I don't see any alternative. The only other alternative is defining 2 methods: One annotated with |
Beta Was this translation helpful? Give feedback.
-
For the sake of discussion (which I DON'T recommend), you could put logic in the template that either checks for the the HX-Request header or for the presence of values in the model. Then the template would include segments of the template based on that logic. I think leveraging the fragment is a better approach. |
Beta Was this translation helpful? Give feedback.
I don't see any alternative. The only other alternative is defining 2 methods: One annotated with
@HxRequest
and one without. But not sure if that is better or not. More a matter of taste I think.