Skip to content

Commit

Permalink
Quick fix (#54)
Browse files Browse the repository at this point in the history
* Fix issues

* Update the instructions
  • Loading branch information
mattesmohr authored Dec 18, 2021
1 parent cfe21ea commit 8ef92b1
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 250 deletions.
7 changes: 4 additions & 3 deletions Instructions/Essential/Context.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ The context is
### Definition

```swift
/// [IndexContext.swift]
/// [SimpleContext.swift]

struct IndexContext {
struct SimpleContext: Codable {

var headline: String
var categories: [Category]
Expand All @@ -21,7 +21,8 @@ struct IndexContext {

struct SimpleView: View {

var context: TemplateValue<IndexContext>
@TemplateValue(SimpleContext.self)
var context

var body: AnyContent {
Heading1 {
Expand Down
45 changes: 37 additions & 8 deletions Instructions/Essential/Elements.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
# Elements

The elements represents the HTML tags. Every element has function handlers, wich are representing the attribute of a tag.
The elements in HTMLKit represent the official HTML elements. Elements with content use the curly brackets, while elements without content use the round brackets. To add attributes to the element use the offered functions.

## Essential

### Definition

```swift
/// Element with content
/// element with content
Body {
...
}

/// Element without content
/// element without content
Input()
```

### Attributes

```swift
/// Attribute with value
/// attribute with value
.class("value")

/// Attribute as toggle
/// attribute as toggle
.required()
```

### Modifier
```swift
/// Modifies if the condition is true
/// modifies if the condition is true
Body {
...
}
.modify(if: condition) {
$0.hidden()
}

/// Modifies if the value is set
/// modifies if the value is set
Body {
...
}
Expand All @@ -45,4 +45,33 @@ Body {
}
```

## Variation
## Limitation

To prevent invalid code, each element owns a element definition. For example the element `Heading1` is a type of `BodyElement`, so it is only allowed to be placed in the `Body` element. See the [wiki](https://github.com/vapor-community/HTMLKit/wiki) for more element definitions.

```swift
/// [BodyElements.swift]

public struct Heading1: ContentNode, BodyElement {
...
}
```

```html
<html>
<head>
<h1>headline</h1> <!-- not valid -->
</head>
<body>
<h1>headline</h1> <!-- valid -->
</body>
</html>
```

If you need to break the limitation, extend the element with the element definition of your choice or define it as `GlobalElement`.

```swift
extension FormContainer: GlobalElement {
...
}
```
17 changes: 11 additions & 6 deletions Instructions/Essential/Layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ struct SimplePage: Page {

var body: AnyContent {
Document(type: .html5)
Head {
Title {
"SimplePage"
Html {
Head {
Title {
"SimplePage"
}
}
Body {
}
}
Body {
}
}
}
Expand All @@ -37,7 +39,8 @@ The view is a partial view of the page. It should be used to display data using

struct SimpleView: View {

var context: TemplateValue<String> = ""
@TemplateValue(SimpleContext.self)
var context

var body: AnyContent {
Heading1 {
Expand All @@ -49,6 +52,8 @@ struct SimpleView: View {

### Context

The context as an object holds the information for the view. ... See [Context](https://github.com/vapor-community/HTMLKit/blob/master/Instructions/Essential/Context.md) to learn more about it.

## Component

The component is a reusable portion of a page. It should be used to break up large files into smaller components.
Expand Down
2 changes: 1 addition & 1 deletion Instructions/Example/Page/Contexts/CreateContext.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct CreateContext {
struct CreateContext: Codable {

var title: String
}
2 changes: 1 addition & 1 deletion Instructions/Example/Page/Contexts/IndexContext.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct IndexContext {
struct IndexContext: Codable {

var title: String
}
16 changes: 13 additions & 3 deletions Instructions/Example/Page/Controllers/SimpleController.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import Vapor
import HTMLKit

// [/simple]
final class SimpleController {

// [/index]
func getIndex(_ request: Request) throws -> EventLoopFuture<View> {

let context = IndexContext(title: "Index")
return SimpleTemplate.IndexView()
.render(with: IndexContext(title: "Index"), for: request)
}

// [/create]
func getCreate(_ request: Request) throws -> EventLoopFuture<View> {

return IndexView().render(with: context, for: request)
return SimpleTemplate.CreateView()
.render(with: CreateContext(title: "Create"), for: request)
}

// [/create/:model]
func postCreate(_ request: Request) throws -> EventLoopFuture<Response> {
return request.redirect(to: "/simple/index")
}
}
7 changes: 5 additions & 2 deletions Instructions/Example/Page/Views/SimpleTemplate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import HTMLKit
import HTMLKitVaporProvider

public enum SimpleTemplate {

public struct IndexView: View {

public var context: TemplateValue<IndexContext>
@TemplateValue(IndexContext.self)
public var context

public var body: AnyContent {
SimplePage {
Expand All @@ -17,7 +19,8 @@ public enum SimpleTemplate {

public struct CreateView: View {

public var context: TemplateValue<CreateContext>
@TemplateValue(CreateContext.self)
public var context

public var body: AnyContent {
SimplePage {
Expand Down
4 changes: 2 additions & 2 deletions Instructions/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add the packages as dependecies to your package.
dependencies: [
...
.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.0")
.package(name: "HTMLKitVaporProvider", url: "https://github.com/vapor-community/htmlkit-vapor-provider.git", from: "1.2.1")
],
targets: [
.target(
Expand All @@ -29,4 +29,4 @@ targets: [

## Vapor 3

Check the provider to learn more about supporting Vapor 3.
Check the [provider](https://github.com/vapor-community/htmlkit-vapor-provider) to learn more about supporting Vapor 3.
17 changes: 10 additions & 7 deletions Instructions/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ By using Swift's powerful language features and a pre-rendering algorithm, HTMLK

## Index
1. Installation

2. Essential
2.1 Elements
2.2 Layouts
2.3 Context
2.4 Statements
2.1 Elements
2.2 Layouts
2.3 Context
2.4 Statements

3. Features
3.1 Localization
3.2 Conversion
3.3 Templating
3.1 Localization
3.2 Conversion
3.3 Templating

4. Example

## References
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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/MatsMoll/htmlkit-vapor-provider.git", from: "1.2.0")
.package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.2.1")
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTMLKit/External/Attributes/BasicAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public protocol AlternateAttribute: AnyAttribute {

extension AlternateAttribute {

internal var key: String { "alternate" }
internal var key: String { "alt" }
}

extension AlternateAttribute where Self: ContentNode {
Expand Down
Loading

0 comments on commit 8ef92b1

Please sign in to comment.