Skip to content

Commit 01aed00

Browse files
authored
Rename var content to var body (#85)
* rename content to body * readme, comments, benchmarks
1 parent c963893 commit 01aed00

File tree

18 files changed

+103
-42
lines changed

18 files changed

+103
-42
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.2.1

Benchmarks/Benchmarks/ElementaryBenchmarks/Benchmarks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ struct MyCustomElement<H: HTML>: HTML {
252252
myContent = content()
253253
}
254254

255-
var content: some HTML {
255+
var body: some HTML {
256256
myContent
257257
}
258258
}
259259

260260
struct MyListItem: HTML {
261261
let number: Int
262262

263-
var content: some HTML {
263+
var body: some HTML {
264264
let isEven = number.isMultiple(of: 2)
265265

266266
li(.id("\(number)")) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.2.1

Examples/HummingbirdDemo/Sources/App/Pages.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct MainLayout<Body: HTML>: HTMLDocument {
2525
}
2626

2727
struct WelcomePage: HTML {
28-
var content: some HTML {
28+
var body: some HTML {
2929
div(.class("flex flex-col gap-4")) {
3030
p {
3131
"This is a simple example of using "
@@ -52,7 +52,7 @@ struct GreetingPage: HTML {
5252
@Environment(requiring: EnvironmentValues.$name) var name
5353
var greetingCount: Int
5454

55-
var content: some HTML {
55+
var body: some HTML {
5656
if greetingCount < 1 {
5757
p(.class("text-red-500")) {
5858
"No greetings to show."

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct MainPage: HTMLDocument {
3535
struct FeatureList: HTML {
3636
var features: [String]
3737

38-
var content: some HTML {
38+
var body: some HTML {
3939
ul {
4040
for feature in features {
4141
li { feature }
@@ -122,7 +122,7 @@ struct List: HTML {
122122
var items: [String]
123123
var importantIndex: Int
124124

125-
var content: some HTML {
125+
var body: some HTML {
126126
// conditional rendering
127127
if items.isEmpty {
128128
p { "No items" }
@@ -142,7 +142,7 @@ struct ListItem: HTML {
142142
var text: String
143143
var isImportant: Bool = false
144144

145-
var content: some HTML {
145+
var body: some HTML {
146146
// conditional attributes
147147
li { text }
148148
.attributes(.class("important"), when: isImportant)
@@ -183,7 +183,7 @@ struct Button: HTML {
183183
var text: String
184184

185185
// by exposing the HTMLTag type information...
186-
var content: some HTML<HTMLTag.input> {
186+
var body: some HTML<HTMLTag.input> {
187187
input(.type(.button), .value(text))
188188
}
189189
}
@@ -209,7 +209,7 @@ div {
209209
}
210210

211211
struct MyComponent: HTML {
212-
var content: some HTML {
212+
var body: some HTML {
213213
AsyncContent {
214214
"So does this: \(await getMoreData())"
215215
}
@@ -245,7 +245,7 @@ struct MyComponent: HTML {
245245
// ... their values can be accessed ...
246246
@Environment(MyValues.$userName) var userName
247247

248-
var content: some HTML {
248+
var body: some HTML {
249249
p { "Hello, \(userName)!" }
250250
}
251251
}

Sources/Elementary/Core/AsyncContent.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
/// The this element can only be rendered in an async context (ie: by calling ``HTML/render(into:chunkSize:)`` or ``HTML/renderAsync()``).
44
/// All HTML tag types (``HTMLElement``) support async content closures in their initializers, so you don't need to use this element directly in most cases.
55
public struct AsyncContent<Content: HTML>: HTML, Sendable {
6+
public typealias Body = Never
7+
public typealias Tag = Content.Tag
8+
69
@usableFromInline
710
var content: @Sendable () async throws -> Content
8-
public typealias Tag = Content.Tag
911

1012
/// Creates a new async HTML element with the specified content.
1113
///

Sources/Elementary/Core/AsyncForEach.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/// }
1313
/// ```
1414
public struct AsyncForEach<Source: AsyncSequence, Content: HTML>: HTML {
15+
public typealias Body = Never
16+
1517
@usableFromInline
1618
var sequence: Source
1719
@usableFromInline

Sources/Elementary/Core/CoreModel.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// struct FeatureList: HTML {
88
/// var features: [String]
99
///
10-
/// var content: some HTML {
10+
/// var body: some HTML {
1111
/// ul {
1212
/// for feature in features {
1313
/// li { feature }
@@ -22,13 +22,18 @@ public protocol HTML<Tag> {
2222
/// The Tag type defines which attributes can be attached to an HTML element.
2323
/// If an element does not represent a specific HTML tag, the Tag type will
2424
/// be ``Swift/Never`` and the element cannot be attributed.
25-
associatedtype Tag: HTMLTagDefinition = Content.Tag
25+
associatedtype Tag: HTMLTagDefinition = Body.Tag
2626

2727
/// The type of the HTML content this component represents.
28-
associatedtype Content: HTML = Never
28+
associatedtype Body: HTML
2929

3030
/// The HTML content of this component.
31-
@HTMLBuilder var content: Content { get }
31+
@HTMLBuilder var body: Body { get }
32+
33+
/// The HTML content of this component.
34+
@HTMLBuilder
35+
@available(*, deprecated, message: "`var content` is deprecated, use `var body` instead")
36+
var content: Body { get }
3237

3338
static func _render<Renderer: _HTMLRendering>(
3439
_ html: consuming Self,
@@ -42,6 +47,21 @@ public protocol HTML<Tag> {
4247
) async throws
4348
}
4449

50+
extension HTML {
51+
@available(*, deprecated, message: "`var content` is deprecated, use `var body` instead")
52+
public var body: Body {
53+
// NOTE: sorry for the change
54+
content
55+
}
56+
57+
@available(*, deprecated, message: "Content was renamed, use Body instead")
58+
public typealias Content = Body
59+
60+
public var content: Body {
61+
fatalError("Please make sure to add a `var body` implementation to your HTML type.")
62+
}
63+
}
64+
4565
/// A type that represents an HTML tag.
4666
public protocol HTMLTagDefinition: Sendable {
4767
/// The name of the HTML tag as it is rendered in an HTML document.
@@ -93,7 +113,7 @@ public extension HTML {
93113
into renderer: inout Renderer,
94114
with context: consuming _RenderingContext
95115
) {
96-
Content._render(html.content, into: &renderer, with: context)
116+
Body._render(html.body, into: &renderer, with: context)
97117
}
98118

99119
@inlinable
@@ -102,7 +122,7 @@ public extension HTML {
102122
into renderer: inout Renderer,
103123
with context: consuming _RenderingContext
104124
) async throws {
105-
try await Content._render(html.content, into: &renderer, with: context)
125+
try await Body._render(html.body, into: &renderer, with: context)
106126
}
107127
}
108128

Sources/Elementary/Core/Environment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/// struct MyNumber: HTML {
1212
/// @Environment(Values.$myNumber) var number
1313
///
14-
/// var content: some HTML {
14+
/// var body: some HTML {
1515
/// p { "\(number)" }
1616
/// }
1717
/// }

Sources/Elementary/Core/Html+Attributes.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ extension HTMLAttribute {
6464
}
6565

6666
public struct _AttributedElement<Content: HTML>: HTML {
67+
public typealias Body = Never
68+
public typealias Tag = Content.Tag
69+
6770
public var content: Content
6871
public var attributes: _AttributeStorage
6972

0 commit comments

Comments
 (0)