A provider that makes it possible to use HTMLKit with Vapor 4. If you are using Vapor 3, check this provider out.
Add the packages as dependecies to your package.
/// [Package.swift]
...
dependencies: [
...
///1. Add the package
.package(name: "HTMLKitVaporProvider", url: "https://github.com/vapor-community/htmlkit-vapor-provider.git", from: "1.2.0")
],
.targets: [
.target(
...
dependencies: [
...
/// 2. Add the product
.product(name: "HTMLKitVaporProvider", package: "HTMLKitVaporProvider")
]
),
...
This will expose HTMLKit on Request
and Application
as shown below.
You can preload a layout to optimize rendering.
/// [configure.swift]
app.htmlkit.add(view: UserTemplate())
/// [SimpleController.swift]
...
/// Returns a Response
try req.htmlkit.render(UserTemplate.self, with: user)
/// Returns a View
try req.htmlkit.render(view: UserTemplate.self, with: user)
/// Returns an EventLoopFuture<View> and will not require the preload
UserTemplate().render(with: user, for: req)
You can also set a localizationPath to enable Lingo
.
/// [configure.swift]
app.htmlkit.localizationPath = "Resources/Localization"
app.htmlkit.defaultLocale = "nb"