Skip to content

Commit

Permalink
added ForEach element (#26)
Browse files Browse the repository at this point in the history
* added ForEach

* use ForEach in example
  • Loading branch information
sliemeobn authored Sep 13, 2024
1 parent 2af5cc4 commit 2d5b57f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import Elementary
import Hummingbird

// ----------------------
// NOTICE
// For integrating with Hummingbird, please use https://github.com/hummingbird-community/hummingbird-elementary
// You will not need the code in this file.
// ----------------------

extension MainLayout: ResponseGenerator {}

struct HTMLResponseBodyWriter: HTMLStreamWriter {
Expand Down
2 changes: 1 addition & 1 deletion Examples/HummingbirdDemo/Sources/App/Pages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct GreetingPage: HTML {
}
} else {
ul {
for i in 0 ..< greetingCount {
ForEach(0 ..< greetingCount) { i in
li {
"Hello there, \(name)!"
// demo of conditional styling (should be done with CSS in real life)
Expand Down
48 changes: 48 additions & 0 deletions Sources/Elementary/Core/ForEach.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// An element that lazily renders HTML for each element in a sequence.
///
/// Using `ForEach` instead of `for ... in` is potentially more efficient when rendering a large number of elements,
/// as the result does not need to be collected into an array before rendering.
///
/// ```swift
/// ForEach(1 ... 100) { index in
/// li { "Item \(index)" }
/// }
/// ```
public struct ForEach<Data, Content>: HTML
where Data: Sequence, Content: HTML
{
var sequence: Data
// TODO: Swift 6 - @Sendable is not ideal here, but currently the response generators for hummingbird/vapor require sendable HTML types
// also, currently there is no good way to conditionally apply Sendable conformance based on closure type
var contentBuilder: @Sendable (Data.Element) -> Content

/// Creates a new `ForEach` element with the given sequence and content builder closure.
///
/// - Parameters:
/// - sequence: A sequence of data to render.
/// - contentBuilder: A closure that builds the HTML content for each element in the sequence.
public init(_ sequence: Data, @HTMLBuilder content contentBuilder: @escaping @Sendable (Data.Element) -> Content) {
self.sequence = sequence
self.contentBuilder = contentBuilder
}

@_spi(Rendering)
public static func _render<Renderer: _HTMLRendering>(_ html: consuming Self, into renderer: inout Renderer, with context: consuming _RenderingContext) {
context.assertNoAttributes(self)

for element in html.sequence {
Content._render(html.contentBuilder(element), into: &renderer, with: copy context)
}
}

@_spi(Rendering)
public static func _render<Renderer: _AsyncHTMLRendering>(_ html: consuming Self, into renderer: inout Renderer, with context: consuming _RenderingContext) async throws {
context.assertNoAttributes(self)

for element in html.sequence {
try await Content._render(html.contentBuilder(element), into: &renderer, with: copy context)
}
}
}

extension ForEach: Sendable where Data: Sendable, Content: Sendable {}
18 changes: 18 additions & 0 deletions Tests/ElementaryTests/CompositionRenderingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ final class CompositionRenderingTests: XCTestCase {
#"<ul><li id="1">one</li><li class="selected" id="2">two</li></ul>"#
)
}

func testRendersForEachWithRange() async throws {
try await HTMLAssertEqual(
ForEach(1 ... 3) { index in
li { "Item \(index)" }
},
#"<li>Item 1</li><li>Item 2</li><li>Item 3</li>"#
)
}

func testRendersForEachWithLazyMap() async throws {
try await HTMLAssertEqual(
ForEach([1, 2, 3].lazy.map { $0 * 10 }) { index in
li { "Item \(index)" }
},
#"<li>Item 10</li><li>Item 20</li><li>Item 30</li>"#
)
}
}

struct MyPage: HTMLDocument {
Expand Down

0 comments on commit 2d5b57f

Please sign in to comment.