Swiftpack.co - vapor-community/HTMLKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vapor-community.
vapor-community/HTMLKit 2.4.5
A type-safe DSL that renders dynamic HTML templates in Swift
⭐️ 331
🕓 15 weeks ago
macOS linux macOS iOS
.package(url: "https://github.com/vapor-community/HTMLKit.git", from: "2.4.5")

HTMLKit

Render dynamic HTML templates in a typesafe and performant way! By using Swift's powerful language features and a pre-rendering algorithm, HTMLKit will render insanely fast templates but also catch bugs that otherwise might occur with other templating options.

Getting Started

Installation

Add the packages as dependecies and targets to your package file.

/// [Package.swift]

...
dependencies: [
    ...
    ///1. Add the packages
    .package(name: "HTMLKit", url: "https://github.com/vapor-community/HTMLKit.git", from: "2.4.5"),
    .package(name: "HTMLKitVaporProvider", url: "https://github.com/vapor-community/htmlkit-vapor-provider.git", from: "1.2.1")
],
targets: [
    .target( 
    ...
        dependencies: [
            ...
            /// 2. Add the products
            .product(name: "HTMLKit", package: "HTMLKit"),
            .product(name: "HTMLKitVaporProvider", package: "HTMLKitVaporProvider")
        ]
    ),
    ...

Read the installation instructions for more information.

Definition

Create a new file in your project. Add the import at the top of your file and declare a new structure. Extend your structure by adding a layout definition to adopt the required properties and methods. Add your content to the body property.

/// [SimplePage.swift]

/// 1. Add the import
import HTMLKit

/// 2. Define a new structure
struct SimplePage: Page {

    /// 3. Add some content
    var body: AnyContent {
        Document(type: .html5)
        Html {
            Head {
                Title { 
                    "SimplePage"
                }
            }
            Body {
                Paragraph {
                    "Hello World!"
                }
            }
        }
    }
}

Implementation

Call the structure you have created in your controller handler and use the render method to render the view for the request.

/// [SimpleController.swift]

...
final class SimpleController {
    ...
    func getPage(req: Request) throws -> EventLoopFuture<View> {
        /// 1. Call the structure
        return SimplePage().render(for: req)
    }
    ...
}

Features

Localization

HTMLKit offers localization by the help of the Lingo framework. See the instructions for more information.

Conversion

You dont have to rewrite your whole codebase to use HTMLKit. HTMLKit offers a converter to translate HTML into HTMLKit. See the instructions for more information.

Validation

HTMLKit is build with the intention to lead you writing valid code. So you don't need to think about it in particular.

Resources

Instructions

See the instructions to learn more about the package and the features.

Components

See the package to extend your experience with HTMLKit. The package is still work in progress, but it will be available soon.

Live Example

See the package of our contributor, how he uses HTMLKit in his website.

GitHub

link
Stars: 331
Last commit: 3 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

2.4.5
15 weeks ago

Patch

Features

Notes

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics