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.3
A type-safe DSL that renders dynamic HTML templates in Swift
⭐️ 315
🕓 1 week ago
macOS linux macOS iOS
.package(url: "https://github.com/vapor-community/HTMLKit.git", from: "2.4.3")

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.0"),
    .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

Conversion

Validation

Resources

Instructions

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

GitHub

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

Release Notes

2.4.3
1 week ago

Minor Fix

Features

Notes

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