swift 蒸气叶循环
swift vapor leaf loop
我正在尝试从带有 swift vapor 的 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)
}
有两个问题:
您在路由中定义了键 'Pizza',并在 .leaf
文件中使用了 'pizza'。叶子区分大小写。
这很可能是 Leaf 中的一个错误。在 #for
和 (
之间引入 space 导致我的应用程序出现问题。
蒸气4
#for(披萨中的披萨):
#endfor
我正在尝试从带有 swift vapor 的 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)
}
有两个问题:
您在路由中定义了键 'Pizza',并在
.leaf
文件中使用了 'pizza'。叶子区分大小写。这很可能是 Leaf 中的一个错误。在
#for
和(
之间引入 space 导致我的应用程序出现问题。
蒸气4
#for(披萨中的披萨):
#endfor