Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sahandnayebaziz/Hypertext

Any-way-you-want-it, type-safe HTML in Swift.
https://github.com/sahandnayebaziz/Hypertext

dsl html markup server server-side-swift swift type-safe

Last synced: 25 days ago
JSON representation

Any-way-you-want-it, type-safe HTML in Swift.

Awesome Lists containing this project

README

        

![header](header.jpg)

Compose valid HTML in Swift any way you want to.

### Usage

````swift

import Hypertext

title { "hello world." }.render()
// hello world.

head { title { "hello world." } }.render()
// hello world.

head { title { "hello world." } }.render(startingWithSpaces: 0, indentingWithSpaces: 2)
//
//
// hello world.
//
//

````

### Requirements
- Swift 3.0+

### Full usage

1. Rendering a tag

```swift

div().render()
//

```

2. Rendering a tag with child text

```swift

div { "hello world." }.render()
//

hello world.


```

3. Rendering a tag with a child tag

```swift

div { img() }.render()
//



```

4. Rendering a tag with multiple child tags

```swift

div { [img(), img(), img()] }.render()
//





```

5. Rendering a tag with attributes

```swift

link(["rel": "stylesheet", "type":"text/css", "href":"./style.css"]).render()
//

```

6. Rendering a tag with attributes and children

```swift

div(["class": "container"]) { "hello world." }
//

hello world.


```

7. Rendering a doctype declaration

```swift
doctype(.html5).render()
//
```

8. Rendering unminified, with newlines and indentation

```swift

head { title { "hello world." } }.render(startingWithSpaces: 0, indentingWithSpaces: 2)
//
//
// hello world.
//
//

```

9. Rendering a tag in a novel way, any way you want to

```swift

func createTitleTag(forPageNamed pageName: String) -> title {
return title { "Hypertext - \(pageName)" }
}

head { createTitleTag(forPageNamed: "Documentation") }.render()
// Hypertext - Documentation

```

10. Rendering a custom tag

```swift

public class myNewTag: tag {

override public var isSelfClosing: Bool {
return true
}
}

myNewTag().render()
//

```

11. Rendering a custom type by adopting the protocol `Renderable`

```swift

extension MyType: Renderable {
public func render() -> String { ... }
public func render(startingWithSpaces: Int, indentingWithSpaces: Int) -> String { ... }
}

```