Underlying generation with libxml2 for always valid results
Built-in convenient builders and modifiers for common cases
Raw HTML integration
Usage
Tree builder
An HTML tree is built by combining Node which is a protocol implemented on Element and String
let tree = Element.html(head: {
Element.metadata(charset: "UTF-8")
Element(name: "title") { "Hello world" }
}, body: {
Element.division {
Element(name: "h1") { "Hello" }
Element.paragraph { "Lorem ipsum dolor sit amet, <consectetur> adipiscing elit, sed & eiusmod." }
}
})
print(tree.renderHTML())
Will output:
<html><head><meta charset="UTF-8"><title>Hello world</title></head><body><div><h1>Hello</h1><p>Lorem ipsum dolor sit amet, <consectetur> adipiscing elit, sed & eiusmod.</p></div></body></html>
Condition and Iteration
Conditions (if, if else, switch) and iterations (for ... in) will work the same as imperative control-flow.
let cond1 = true
let cond2 = false
let elements = ["Lorem", "ipsum"]
let treeControlFlow = Element.html(head: {
Element.metadata(charset: "UTF-8")
Element(name: "title") { "Hello world" }
}, body: {
Element.division {
if cond1 {
Element(name: "h1") { "Hello" }
}
if cond2 {
Element(name: "h1") { "Hello" }
} else {
Element(name: "h1") { "world" }
}
ForEach(elements) { el in
Element.paragraph { el }
}
}
})
print(treeControlFlow.renderHTML())
HTMLBuilder
Swift library for generating HTML using Result Builder
Features
Usage
Tree builder
An HTML tree is built by combining
Node
which is a protocol implemented onElement
andString
Will output:
Condition and Iteration
Conditions (
if
,if else
,switch
) and iterations (for ... in
) will work the same as imperative control-flow.Will output:
Modifiers
Element
is modifiable while building the tree. There is 3 built-in modifiers,identifier
,class
andattributes
Will output:
attributes
takes a closure withinout
attributes that you can modify:Will output:
Raw HTML
You can include raw html in the tree with
RawHTML
Will output: