Replies: 3 comments
-
Excellent question, but yes it's possible to generate a document from smaller pieces. It's not super obvious though and indeed some more docs and examples would be welcome. import Html
let body: ChildOf<Tag.Html> = .body(
.h1("Welcome!"),
.p("You’ve found our site!")
)
let head: ChildOf<Tag.Html> = .head(
.meta(attributes: [.charset(.utf8)]),
.title("Hello")
)
let document: Node = .document(
.html(
head,
body
)
)
print(render(document)) As for your other question, that This: let body: ChildOf<Tag.Html> = .body(
.h1("Welcome!"),
.p("You’ve found our site!")
) Is the same as this: let body: ChildOf<Tag.Html> = ChildOf<Tag.Html>.body(
Node.h1("Welcome!"),
Node.p("You’ve found our site!")
) But the compiler can figure out that first part so you can leave it off. |
Beta Was this translation helpful? Give feedback.
-
How would you build out a TBody based on values in an array? |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion.
@MHDallas You can use Node.table(
.tbody(
.fragment(
[1, 2, 3].map { n in
.tr(
.td(.text("\(n)"))
)
}
)
)
) |
Beta Was this translation helpful? Give feedback.
-
So far all of the examples I've seen are for building up a
.document
from a complete set of sub-nodes all at once, but realistically a web page is going to be built up dynamically. I want to create a element and adjust the sub-tags based on logic in the program, then build up the piece by piece, and then finally assemble into.html
and into a final.document
for rendering.After some exploration in the documentation and test methods I am for instance able to create an
.html
node like this:But then why can't I do something similar to create a
.head
like this?That has 3 compile errors!
Maybe there is a workaround by avoiding the "helper functions" but it would be so nice to be able to manipulate fragments with the helper functions and then assemble them into a full
.document
(or fragment) as needed.BTW is the dot-syntax you are using here in the Swift language documented somewhere? It doesn't seem to be like functional programming where "." is used to chain the result of one function into another function (e.g. in SwiftUI), but something different altogether. If you can point your users to a starting point here, it might be easier to figure out how to make use of the framework beyond the surface level. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions