-
I just started looking at Elementary. So I apologize if this is a dumb question. It seems very straightforward when taking a swift property like an array of strings and using them in a defined html structure. But what if I need to take html as a property? Is this the correct way to do it? The code compiles. I just don't know if this is the right approach. Thanks in advance. public struct MyDiv: HTML {
public var features: [String]
public var supplied: Content
public init(
features: [String],
@HTMLBuilder content: () -> Content
) {
self.features = features
self.supplied = content()
}
public var content: some HTML {
div {
supplied.render()
ul {
for feature in features {
li { feature }
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
you almost got it right, but you need to make you can see this in practice in the example project (under Examples/HummingbirdDemo/Sources/App/Pages.swift) your snippet should look something like this: public struct MyDiv<Supplied: HTML>: HTML {
public var features: [String]
public var supplied: Supplied
public init(
features: [String],
@HTMLBuilder content: () -> Supplied
) {
self.features = features
self.supplied = content()
}
public var content: some HTML {
div {
supplied
ul {
for feature in features {
li { feature }
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
you almost got it right, but you need to make
MyDiv
generic over the type ofsupplied
.In your case, you are using the type
Content
, which is the associated type of theHTML
protocol (ie: whatvar content
returns) - that is why the swift type checker fails to figure out what is going on ; )also, no need to call
.render()
in there.you can see this in practice in the example project (under Examples/HummingbirdDemo/Sources/App/Pages.swift)
your snippet should look something like this: