Learn how to integrate Leaf into your application and start injecting data into Leaf templates.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/18
Learn how to integrate Leaf into your application and start injecting data into Leaf templates.
In the beginning when you say that normally you should split it into an api application and web application is this normal practice? I ask because Iâve used previous frameworks (i.e. Laravel) and it has their templating engine built in as well.
So would there be 2 separate vapor projects where one only takes care of the api (what we just learned from the previous sections) and the other where itâs only the frontend that communicates with the api portion?
Unless Iâm missing something, why not put everything together as the leaf templating would be separated from the api files?
@simonqq yeah exactly. In a âmodernâ web application you would generally have your front-end (which could be running Leaf) on www.mysite.com. That then makes calls to api.mysite.com either through Javascript or in the Vapor application running the Leaf site.
The reason why you tend to do this is because you donât want a bit monolithic application that canât scale very well. Letâs say you have an iOS application, if that gets really popular then you need to grow the API. You donât want to waste resources building and then running extra front end sites as well. It also means that it is easier to add new features and fix bugs in smaller applications than one large application. It reduces source-control conflicts, typically it would be different dev teams running the API and front-end and co-ordinating releases etc can be difficult. Thatâs why there has been a big push to microservices over the last few years. Of course you can have common code, so business logic and models etc can all be in a shared module so you donât duplicate yourself etc.
It is all a matter of scale. For a simple application like the TIL app, there is little point splitting it out. It is just easier to put everything together. If you are building Facebook you donât want everything in the same application! You just need to find the balance.
Extra argument âtemplateâ in call in LeafParcer.swift/:160? How can I add leaf to the project and avoid this mistake?
yes, right until leaf all worked good
It doesnât generate project using â3.0.0-beta.1â, project is generated with â3.0.0-rc.1â but I got 21 issues in LeafParcer.swift with Extra argument âtemplateâ in call
@aktrader thatâs bizarre! First in terminal in the project directory can you try running rm -rf .build TILApp.xcodeproj && swift package update && vapor xcode -y
and see if that fixes it?
Yes it worked, thanks
Hi @0xtim, Iâve a problem on âvapor xcodeâ command.
This is the error:
Error: Could not generate Xcode project: error: terminated(128): git -C /Users/michelemola/Downloads/2-getting-started-with-leaf/part2/TILApp/.build/repositories/console.gitâ2431895819212044213 rev-parse --verify âbeta^{commit}â
error: product dependency âVaporâ not found
error: product dependency âFluentMySQLâ not found
error: product dependency âLeafâ not found
And this is my Package.swift:
import PackageDescription
let package = Package(
name: âTILAppâ,
dependencies: [
// A server-side Swift web framework.
.package(url: âGitHub - vapor/vapor: đ§ A server-side Swift HTTP web framework.â, .exact(â3.0.0-beta.2â)),
.package(url: âGitHub - vapor/leaf: đ An expressive, performant, and extensible templating language built for Swift.â, .exact(â3.0.0-beta.2â)),
.package(url: âGitHub - vapor/fluent: Vapor ORM (queries, models, and relations) for NoSQL and SQL databasesâ, .exact(â3.0.0-beta.2â)),
.package(url: âGitHub - vapor/fluent-mysql-driver: đđŹ Swift ORM (queries, models, relations, etc) built on MySQL.â, .exact(â3.0.0-beta.2â)),
],
targets: [
.target(name: âAppâ, dependencies: [âFluentMySQLâ, âVaporâ, âLeafâ]),
.target(name: âRunâ, dependencies: [âAppâ]),
.testTarget(name: âAppTestsâ, dependencies: [âAppâ]),
]
)
@michele your Package.swift
may need updating. Try changing all the dependencies from .exact("3.0.0-beta.2")
to from: "3.0.0-rc"
like the current template. Then do a clean to remove any issues:
rm -rf Package.resolved .build
vapor xcode -y
Hopefully that should fix any issues!
Thanks you for your attention, it works.
Best regards,
Michele Mola.
When I try to run after Iâve configured leaf with the new directory I go tthis.
services.register { container -> LeafConfig in
let dir = try container.make(DirectoryConfig.self)
return try LeafConfig(
tags: container.make(),
viewsDir: dir.workDir + "Resources/Views",
shouldCache: container.environment != .development
)
}
âUse of unresolved identifier âDirectoryConfigââ
I sort of solved it by having LeafProvider.swift import Core, but of course that broke other stuff.
All seems fixed when you add import Core to that dependancy. I think itâs missing for now until you fix it and send it out.
@yungdai my PR has been merged and tagged, so if you run vapor update
it should pull down the latest code and fix the issue
Awesome! Really looking forward to your book! Itâs the final key to a custom App iâm making.
Instead of the leaf() extension you wrote, why not just take it all the way?
func renderLeaf<T>(_ path: String, _ context: T) throws -> NIO.EventLoopFuture<TemplateKit.View> where T : Encodable {
return try self.make(LeafRenderer.self).render(path, context)
}
Then you can call it like return try req.renderLeaf("clients", context)
Yep, you can do that, though probably simplify it a bit:
func renderLeaf(_ path: String, _ context: T? = nil) throws -> Future<View> where T : Encodable {
return try self.make(LeafRenderer.self).render(path, context)
}