diff --git a/README.md b/README.md index 41da32c9..d3727b77 100644 --- a/README.md +++ b/README.md @@ -11,36 +11,37 @@ By using Swift's powerful language features and a pre-rendering algorithm, HTMLK Add the following in your `Package.swift` file ```swift -.package(url: "https://github.com/vapor-community/HTMLKit.git", from: "2.0.0-alpha.9"), +.package(url: "https://github.com/vapor-community/HTMLKit.git", from: "2.0.0-beta.2"), ``` -And register the provider and the different templates with in `configure.swift` -```swift -try services.register(HTMLKitProvider()) -// Or you can do it manually with -let renderer = HTMLRenderer() -try renderer.add(view: MyTemplate()) -services.register(renderer) -``` +You can use the following providers in order to use HTMLKit with [Vapor 3](https://github.com/MatsMoll/htmlkit-vapor-3-provider) and for [Vapor 4](https://github.com/MatsMoll/htmlkit-vapor-provider) ## Usage To create a HTML template, conform to the `HTMLTemplate` protocol. ```swift +struct TemplateData { + let name: String + let handle: String + let title: String? +} + struct SimpleTemplate: HTMLTemplate { - @TemplateValue(String?.self) + @TemplateValue(TemplateData.self) var context var body: HTML { Document(type: .html5) { Head { - Title { context } + Title { context.title } + Author { context.name } + .twitter(handle: context.handle) } Body { - Unwrap(context) { string in - P { string } + Unwrap(context.title) { title in + P { title } } } } @@ -51,6 +52,23 @@ struct SimpleTemplate: HTMLTemplate { try SimpleTemplate().render(with: "Some string", for: req) ``` +This will render somehing like this +```html + + + + Some Title + + + + + + +

Some Title

+ + +``` + And to create a HTML component, just comform to the `HTMLComponent` protocol. ```swift @@ -89,7 +107,7 @@ struct SomePage: HTMLPage { var body: HTML { Div { Alert(isDismissable: false) { - H3("Some Title") + H3 { "Some Title" } } } } @@ -164,3 +182,7 @@ struct LocalizedDateView: HTMLTemplate { * [BootstrapKit](https://github.com/MatsMoll/BootstrapKit) - A higher level library that makes it easier to work with Boostrap 4. * [Vapor TIL fork](https://github.com/MatsMoll/vapor-til) - Compare Leaf to HTMLKit in this fork of the TIL app. * Convert pure HTML code to HTMLKit code using this [HTML-to-HTMLKit converter](https://github.com/MatsMoll/HTMLKit-code-converter). + +## Known Issues + +* Linux compiler can sometimes struggle with compiling the code when a combination of complex use of generics, default values or deeply nested function builders are used. diff --git a/Sources/HTMLKit/HTMLDocument.swift b/Sources/HTMLKit/HTMLDocument.swift index e9ea15d5..f6fc9a4f 100644 --- a/Sources/HTMLKit/HTMLDocument.swift +++ b/Sources/HTMLKit/HTMLDocument.swift @@ -35,6 +35,9 @@ public struct Document: HTMLPage { } public var body: HTML { - "" + content + [ + "", + HTMLNode { content } + ] } } diff --git a/Tests/HTMLKitTests/HTMLKitTests.swift b/Tests/HTMLKitTests/HTMLKitTests.swift index 7a02a545..3c94472f 100644 --- a/Tests/HTMLKitTests/HTMLKitTests.swift +++ b/Tests/HTMLKitTests/HTMLKitTests.swift @@ -173,11 +173,6 @@ final class HTMLKitTests: XCTestCase { // } // } - func testMakeOptional() throws { - let metadataPageDynamic = try renderer.render(raw: MetadataTestDynamic.self, with: .init(name: "Mats", handle: "@MatsMoll")) - XCTAssertEqual(metadataPageDynamic, "Some title") - } - func testHtmlRenderingTests() throws { let testDate = Date() @@ -248,8 +243,8 @@ final class HTMLKitTests: XCTestCase { // XCTAssertEqual(markdown.replacingOccurrences(of: "\n", with: ""), "

Title: Hello

Description here:

World

") XCTAssertEqual(english, "

Hello World!

You have 3 unread messages.

You have 2 unread messages.

You have an unread message

") XCTAssertEqual(norwegian, "

Hei Verden!

Du har 3 uleste meldinger.

Du har 2 uleste meldinger.

Du har en ulest melding

") - XCTAssertEqual(metadataPage, "Some title") - XCTAssertEqual(metadataPageDynamic, "Some title") + XCTAssertEqual(metadataPage, "Some title") + XCTAssertEqual(metadataPageDynamic, "Some title") XCTAssertEqual(date, "

\(shortDateFormatter.string(from: testDate))

\(customDateFormatter.string(from: testDate))

") XCTAssertEqual(optionalDateNil, "

") XCTAssertEqual(optionalDate, "

\(shortDateFormatter.string(from: testDate))

\(customDateFormatter.string(from: testDate))

") diff --git a/Tests/HTMLKitTests/HTMLTestDocuments.swift b/Tests/HTMLKitTests/HTMLTestDocuments.swift index ba9fec17..4f2ef8c6 100644 --- a/Tests/HTMLKitTests/HTMLTestDocuments.swift +++ b/Tests/HTMLKitTests/HTMLTestDocuments.swift @@ -75,19 +75,17 @@ struct BaseView: HTMLComponent { var body: HTML { Document(type: .html5) { - HTMLNode { - Head { - Title { context } - .useTwitter(metadata: false) - .useOpenGraph(metadata: false) - Stylesheet(url: "some url") - Meta() - .name(.viewport) - .content("width=device-width, initial-scale=1.0") - } - Body { - content - } + Head { + Title { context } + .useTwitter(metadata: false) + .useOpenGraph(metadata: false) + Stylesheet(url: "some url") + Meta() + .name(.viewport) + .content("width=device-width, initial-scale=1.0") + } + Body { + content } } }