[英]swift vapor leaf loop
I am trying to get a very simple result from a leaf renderer for a for loop with swift vapor.我试图从叶渲染器获得一个非常简单的结果,用于快速蒸汽的 for 循环。
I am uploading leaf file HTML as it is not accepting the code here in correct format -我正在上传叶文件 HTML,因为它不接受此处格式正确的代码 -
pizza.swift code below -下面的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 code below - route.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)
}
what I expect is -我期望的是——
Pizza比萨
Welcome to best pizza in the world欢迎来到世界上最好的披萨
.statinisland
.rome
what I get is -我得到的是 -
Pizza比萨
Welcome to best pizza in the world欢迎来到世界上最好的披萨
.#for (pizza in pizza) {
.
}
If you need more information I can upload.如果您需要更多信息,我可以上传。 Any help will be appreciated.
任何帮助将不胜感激。
edit - I am also adding configure.swift code below -编辑 - 我也在下面添加 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)
}
There are two issues:有两个问题:
You defined a key 'Pizza' in your route and used 'pizza' in your .leaf
file.您在路线中定义了一个关键的“披萨”,并在
.leaf
文件中使用了“披萨”。 Leaf is case-sensitive.叶区分大小写。
This may well be a bug in Leaf.这很可能是 Leaf 中的一个错误。 Introducing a space between
#for
and (
caused a problem with a working app for me.在
#for
和(
之间引入一个空格会导致我的工作应用出现问题。
in vapor4蒸气4
#for(pizza in pizzas): #for(比萨饼中的比萨饼):
#endfor #endfor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.