Skip to content

Commit 1a9e5a9

Browse files
relaxed requiring innerHTML label; updated README
1 parent 7b2b989 commit 1a9e5a9

File tree

4 files changed

+166
-144
lines changed

4 files changed

+166
-144
lines changed

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ Write HTML using Swift Macros.
33
## Why Use?
44
- Swift Macros are powerful and offer performance benefits to alternative libraries relying on runtime performance (middleware, rendering, result builders, etc)
55
- Alternative libraries may not fit all situations and may restrict how the html is generated or manipulated, or is prone to html errors (by human error or otherwise)
6-
- HTML macros enforces safety by default, can be used anywhere, and compile directly to strings which can be easily manipulated
6+
- HTML macros enforce safety, can be used anywhere, and compile directly to strings which are easily manipulated
77
- The compiled output is valid, minified html
8-
## Syntax
9-
- prepend the html element you want with the macro delimiter (`#<html element name>()`)
10-
- create `html` element: `#html(innerHTML: [])`
11-
- create `body` element: `#body()`
12-
- create `div` element: `#div()`
13-
- create `div` element with "dark" and "mode" classes: `#div(attributes: [.class(["dark", "mode"])])`
14-
### Example
15-
This html is compiled by the following code
16-
```html
17-
<!DOCTYPE html><html><body><div class="dark mode" title="Hover over message" draggable="false" inputmode="email" hidden="hidden">Unconstrained text<div></div><a><div><abbr></abbr></div></a><div></div><button disabled></button><video autoplay preload="auto" src="ezclap" width="5"></video></div></body></html>
18-
```
8+
### Examples
199
```swift
20-
let test:String = #html(innerHTML: [
21-
#body(innerHTML: [
10+
let test:String = #html([
11+
#body([
2212
#div(
2313
attributes: [
2414
.class(["dark", "mode"]),
@@ -27,10 +17,10 @@ let test:String = #html(innerHTML: [
2717
.inputMode(.email),
2818
.hidden(.hidden)
2919
],
30-
innerHTML: [
31-
"Unconstrained text",
20+
[
21+
"Random text",
3222
#div(),
33-
#a(innerHTML: [#div(innerHTML: [#abbr()])]),
23+
#a([#div([#abbr()])]),
3424
#div(),
3525
#button(disabled: true),
3626
#video(autoplay: true, controls: false, height: nil, preload: .auto, src: "ezclap", width: 5),
@@ -39,3 +29,19 @@ let test:String = #html(innerHTML: [
3929
])
4030
])
4131
```
32+
```swift
33+
func testExample2() {
34+
var test:TestStruct = TestStruct(name: "one", array: ["1", "2", "3"])
35+
XCTAssertEqual(test.html, "<p>one123</p>")
36+
37+
test.name = "two"
38+
test.array = [4, 5, 6, 7, 8]
39+
XCTAssertEqual(test.html, "<p>two45678</p>")
40+
}
41+
struct TestStruct {
42+
var name:String
43+
var array:[CustomStringConvertible]
44+
45+
var html : String { #p(["\(name)", "\(array.map({ "\($0)" }).joined())"]) }
46+
}
47+
```

0 commit comments

Comments
 (0)