Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/randomhashtags/swift-htmlkit
Write HTML using Swift Macros.
https://github.com/randomhashtags/swift-htmlkit
dsl html server-side-swift swift swift-library swift-macro swift-macros
Last synced: 3 days ago
JSON representation
Write HTML using Swift Macros.
- Host: GitHub
- URL: https://github.com/randomhashtags/swift-htmlkit
- Owner: RandomHashTags
- License: apache-2.0
- Created: 2024-09-16T18:21:00.000Z (11 days ago)
- Default Branch: main
- Last Pushed: 2024-09-22T22:25:34.000Z (5 days ago)
- Last Synced: 2024-09-25T01:13:00.627Z (3 days ago)
- Topics: dsl, html, server-side-swift, swift, swift-library, swift-macro, swift-macros
- Language: Swift
- Homepage:
- Size: 108 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Write HTML using Swift Macros.
## Why?
- Swift Macros are powerful and offer performance benefits
- Alternative libraries may not fit all situations and may restrict how the html is generated/manipulated, prone to human error, or cost a constant performance overhead (middleware, rendering, result builders, etc)
- HTML macros enforce safety, can be used anywhere, and compile directly to strings which are easily manipulated
- The output is minified at no performance cost
## Examples
```swift
let test:String = #html([
#body([
#div(
attributes: [
.class(["dark-mode", "row"]),
.draggable(.false),
.hidden(.true),
.inputmode(.email),
.title("Hey, you're pretty cool")
],
[
"Random text",
#div(),
#a([
#div([
#abbr()
]),
#address()
]),
#div(),
#button(disabled: true),
#video(autoplay: true, controls: false, preload: .auto, src: "https://github.com/RandomHashTags/litleagues", width: .centimeters(1)),
]
)
])
])
```
```swift
func testExample2() {
var test:TestStruct = TestStruct(name: "one", array: ["1", "2", "3"])
XCTAssertEqual(test.html, "one123
")
test.name = "two"
test.array = [4, 5, 6, 7, 8]
XCTAssertEqual(test.html, "two45678
")
}
struct TestStruct {
var name:String
var array:[CustomStringConvertible]
var html : String { #p(["\(name)", "\(array.map({ "\($0)" }).joined())"]) }
}
```