Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vapor-community/HTMLKit
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsMoll committed Sep 22, 2020
2 parents 1a5d872 + d32651f commit c2eccd1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ Add the following in your `Package.swift` file

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)

for instance for Vapor 4, you have to add the **HTMLKit** vapor provider like this

```swift
.package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.0.0-beta.4"),
...
.product(name: "HTMLKit", package: "HTMLKit"),
.product(name: "HTMLKitVaporProvider", package: "HTMLKitVaporProvider"),
```

## Usage

To create a HTML template, conform to the `HTMLTemplate` protocol.
Expand Down Expand Up @@ -49,11 +58,31 @@ struct SimpleTemplate: HTMLTemplate {
}
}

// For Vapor 4, you certainly want to preload the template in configure to optimize rendering.
// in configure.swift
import HTMLKitVaporProvider
...
try SimpleTemplate().render(with: .init(...), for: req)
public func configure(_ app: Application) throws {
...
try app.htmlkit.add(view: SimpleTemplate())
...
}

// In one of your controllers
import HTMLKit
...
func get(req: Request) throws -> EventLoopFuture<View> {
return Simple.query(on: req.db)
.filter(...)
.first()
.unwrap(or: Abort(.notFound))
.flatMap {
SimpleTemplate().render(with: TemplateData(name: $0.name, ...), for: req)
}
}
```

This will render somehing like this
This will render something like this
```html
<!DOCTYPE html>
<html>
Expand All @@ -71,19 +100,19 @@ This will render somehing like this
</html>
```

And to create a HTML component, just comform to the `HTMLComponent` protocol.
And to create a HTML component, just conform to the `HTMLComponent` protocol.

```swift
public struct Alert: HTMLComponent {

let isDisimissable: Conditionable // This is a protocol that makes it possible to optimize if's
let isDismissable: Conditionable // This is a protocol that makes it possible to optimize if's
let message: HTML

public var body: HTML {
Div {
message
IF(isDisimissable) {

IF(isDismissable) {
Button {
Span { "&times;" }
.aria(for: "hidden", value: true)
Expand All @@ -95,7 +124,7 @@ public struct Alert: HTMLComponent {
}
}
.class("alert alert-danger bg-danger")
.modify(if: isDisimissable) {
.modify(if: isDismissable) {
$0.class("fade show")
}
.role("alert")
Expand Down Expand Up @@ -154,7 +183,7 @@ var renderer = HTMLRenderer()
try renderer.registerLocalization(atPath: "workDir", defaultLocale: "en")
```
And if the locale changes based on some user input, then you can change the used locale in the template.
This also effects how dates are presentet to the user.
This also effects how dates are presented to the user.
```swift
struct LocalizedDateView: HTMLTemplate {

Expand Down
6 changes: 3 additions & 3 deletions Sources/HTMLKit/HTMLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public protocol HTMLRenderable {
func render<T: HTMLPage>(raw type: T.Type) throws -> String
}

/// A struct containing the differnet formulas for the different views.
/// A struct containing the different formulas for the different views.
///
/// try renderer.add(template: WelcomeView()) // Builds the formula
/// try renderer.render(WelcomeView.self) // Renders the formula
Expand Down Expand Up @@ -174,11 +174,11 @@ public class HTMLRenderer: HTMLRenderable {
lingo = try Lingo(rootPath: path, defaultLocale: defaultLocale)
}

/// Manage the differnet contexts
/// Manage the different contexts
/// This will remove the generic type in the render call
public class ContextManager<Context> {

/// The different context varaibles used when rendering
/// The different context variables used when rendering
var contexts: [String: Any]

/// The lingo object that is needed to use localization
Expand Down

0 comments on commit c2eccd1

Please sign in to comment.