0

我创建了 Codable struct Person。我尝试将此结构的对象数组添加到模板的上下文中(参见代码)。但它不起作用。

怎么做对?


struct Person: Codable {
    public var name: String
    public var age: UInt8
    public var description: String
    public var photoURL: String

    init(name: String, age: UInt8, description: String, photoURL: String) {
        self.name = name
        self.age = age
        self.description = description
        self.photoURL = photoURL
    }

    public static let allPersons = [
        Person(name: "Name 1", age: 18, description: "Cool man", photoURL: "https://photo.ur/1"),
        Person(name: "Name 2", age: 21, description: "Designer", photoURL: "https://photo.url/2")
    ]
}

struct MainData: Encodable {
    var products: [String]
    var persons: [Person]
}

try response.render("home.stencil", with: MainData(
    products: [ "Music", "Fashion", "etc" ],
    persons: Person.allPersons // static array with persons
))

[错误] [RouterMiddlewareWalker.swift:72 next()] 无法渲染上下文(上下文:)

PS 已解决。我在模板模板中有语法错误。

4

0 回答 0