Skip to content

Consider adding the ability to use a dictionary as a view context #228

@fwgreen

Description

@fwgreen

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.

import Vapor
import AnyCodable

extension ViewRenderer {
    public func render(_ name: String, _ context: [String: Any]) async throws -> View {
        let data = context.reduce(into: [:]) { $0[$1.0] = AnyCodable($1.1) }
        return try await self.render(name, data)
    }
}

Maybe making Leaf's builtin encoder public might be a possible compromise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions