-
-
Notifications
You must be signed in to change notification settings - Fork 82
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
Consider adding the ability to use a dictionary as a view context #228
Comments
This is already possible, see https://github.com/vapor/leaf/blob/main/Tests/LeafTests/LeafTests.swift#L20 for an example. What are you running into that's preventing you from doing this? |
That test seems to only cover |
What's wrong with creating an inline |
There's nothing wrong with that approach, and I'm glad to have the option. The problem is being forced to find a unique name for a view context which doesn't require a name. It's fine for small projects but becomes unwieldy for larger ones. I'm assuming that's why, at least in the Java world, most (if not all) frameworks offer some sort of HashMap backed model type for this purpose. |
Additionally the use of func someRouteHandler(_ req: Request) async throws -> View {
struct Context: Encodable {
// Whatever you want here
}
let context = Context(...)
return try await req.view.render("template", context)
} That provides the flexibility without needing to extract and name everything (though I'm still a big proponent for that, especially in large projects as it makes it clear what's needed and makes it easier to test). I think any addition would need to reach quite a high bar given the current APIs available |
@0xTim My apologies if my choice of words made it seem as if I wanted to include |
@fwgreen which parts to you want made public? |
(Just to clarify, we could probably make |
Making |
This might be a welcomed alternative to finding a unique name for something that, in most situations, doesn't require naming. Plus it has the added benefit of being familiar to someone coming from one of the many frameworks that use this approach.
My current approach, sadly, uses
AnyCodable
as Leaf's encoder isn't public.Maybe making Leaf's builtin encoder public might be a possible compromise.
The text was updated successfully, but these errors were encountered: