Search code examples
swiftloopsvaporleaf

swift vapor leaf loop


I am trying to get a very simple result from a leaf renderer for a for loop with swift vapor.

I am uploading leaf file HTML as it is not accepting the code here in correct format -

enter image description here

pizza.swift code below -

   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 -

   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 -

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

Solution

  • There are two issues:

    1. You defined a key 'Pizza' in your route and used 'pizza' in your .leaf file. Leaf is case-sensitive.

    2. This may well be a bug in Leaf. Introducing a space between #for and ( caused a problem with a working app for me.