How to add array of objects to stencil context?

299 views Asked by At

I created Codable struct Person. And I tried to add array of objects of this struct to stencil's context (See code down). But it doesn't work.

How do it right?


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
))

[ERROR] [RouterMiddlewareWalker.swift:72 next()] unableToRenderContext(context:)

P.S. Resolved. I had a syntax error in stencil template.

0

There are 0 answers