Compose valid HTML in Swift any way you want to.
import Hypertext
title { "hello world." }.render()
// <title>hello world.</title>
head { title { "hello world." } }.render()
// <head><title>hello world.</title></head>
head { title { "hello world." } }.render(startingWithSpaces: 0, indentingWithSpaces: 2)
// <head>
// <title>
// hello world.
// </title>
// </head>
- Swift 3.0+
-
Rendering a tag
div().render() // <div></div>
-
Rendering a tag with child text
div { "hello world." }.render() // <div>hello world.</div>
-
Rendering a tag with a child tag
div { img() }.render() // <div><img/></div>
-
Rendering a tag with multiple child tags
div { [img(), img(), img()] }.render() // <div><img/><img/><img/></div>
-
Rendering a tag with attributes
link(["rel": "stylesheet", "type":"text/css", "href":"./style.css"]).render() // <link rel="stylesheet" type="text/css" href="./style.css"/>
-
Rendering a tag with attributes and children
div(["class": "container"]) { "hello world." } // <div class="container">hello world.</div>
-
Rendering a doctype declaration
doctype(.html5).render() // <!DOCTYPE html>
-
Rendering unminified, with newlines and indentation
head { title { "hello world." } }.render(startingWithSpaces: 0, indentingWithSpaces: 2) // <head> // <title> // hello world. // </title> // </head>
-
Rendering a tag in a novel way, any way you want to
func createTitleTag(forPageNamed pageName: String) -> title { return title { "Hypertext - \(pageName)" } } head { createTitleTag(forPageNamed: "Documentation") }.render() // <head><title>Hypertext - Documentation</title></head>
-
Rendering a custom tag
public class myNewTag: tag { override public var isSelfClosing: Bool { return true } } myNewTag().render() // <my-new-tag/>
-
Rendering a custom type by adopting the protocol
Renderable
extension MyType: Renderable { public func render() -> String { ... } public func render(startingWithSpaces: Int, indentingWithSpaces: Int) -> String { ... } }