我试图从叶子渲染器中获得一个非常简单的结果,用于快速蒸汽的 for 循环。
我正在上传叶子文件 HTML,因为它不接受此处格式正确的代码 -
Pizza.swift 代码如下 -
import Foundation
import Vapor
struct pizza: Encodable, Content, Decodable {
var id: Int?
var name: String
var description: String
var price: Int
}
下面的 routes.swift 代码 -
import Routing
import Vapor
public func routes(_ router: Router) throws {
router.get { req -> Future <View> in
let Newyorker = pizza(id: 5, name: "statinisland", description: "impracticaljokers", price: 55)
let Traditional = pizza(id: 5, name: "rome", description: "pope", price: 55)
return try req.view().render("welcome",["Pizza":[Newyorker,Traditional]])
}
// Example of configuring a controller
let todoController = TodoController()
router.get("todos", use: todoController.index)
router.post("todos", use: todoController.create)
router.delete("todos", Todo.parameter, use: todoController.delete)
}
我期望的是——
比萨
欢迎来到世界上最好的披萨
.statinisland
.rome
我得到的是-
比萨
欢迎来到世界上最好的披萨
.#for (pizza in pizza) {
.
}
如果您需要更多信息,我可以上传。任何帮助将不胜感激。
编辑 - 我还在下面添加 configure.swift 代码 -
import FluentSQLite
import Vapor
import Leaf // added
/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
// Register providers first
try services.register(FluentSQLiteProvider())
// Register routes to the router
let router = EngineRouter.default()
try routes(router)
services.register(router, as: Router.self)
// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
// middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
services.register(middlewares)
// Configure a SQLite database
let sqlite = try SQLiteDatabase(storage: .memory)
// Register the configured SQLite database to the database config.
var databases = DatabasesConfig()
databases.add(database: sqlite, as: .sqlite)
services.register(databases)
// Configure migrations
var migrations = MigrationConfig()
migrations.add(model: Todo.self, database: .sqlite)
services.register(migrations)
let leafProvider = LeafProvider() // added
try services.register(leafProvider) // added
config.prefer(LeafRenderer.self, for: ViewRenderer.self)// added
// http://localhost:8080/ already in use so adding new server http://localhost:8080/ below -
let serverConfigure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
services.register(serverConfigure)
}