More Routing Options Needed / Components #9
Replies: 6 comments 2 replies
-
Hi @stunjiturner, I'm not sure I understand the question here. Can you clarify a bit more? |
Beta Was this translation helpful? Give feedback.
-
@stunjiturner I see two parts in the original issue statement. First, the <html>
<head>
<!-- -->
</head>
<body>
<!-- some menu navigation here -->
<!-- ## COMMON HEADER ABOVE HERE ## -->
<!-- PAGE CONTENT: page_a, page_b, page_etc, ... -->
<!-- ## COMMON FOOTER BELOW HERE ## -->
<!-- may include some javascript -->
</body>
</html> Extending/adapting the Second, |
Beta Was this translation helpful? Give feedback.
-
Hi @stunjiturner and @marc-medley, sorry for the delay in response. Here's a bit of info on how to accomplish what you want. The nice thing having your HTML as simple swift data structures is that you can combine them in the same ways you might combine arrays and dictionaries. In particular, we could create a function that takes the main body of the page as an argument, and returns a new node that represents the page with the header and footer included: func layout(mainContent: Node) -> Node {
return .html(
.head(
// whatever you want in here
),
.body(
.header(
.ul(
.li(.a(attributes: [.href("/")], "Home")),
.li(.a(attributes: [.href("/contact")], "Contact us")),
.li(.a(attributes: [.href("/about")], "About"))
)
),
.main(
mainContent
)
.footer(
.ul(
.li(.a(attributes: [.href("/")], "Home")),
.li(.a(attributes: [.href("/contact")], "Contact us")),
.li(.a(attributes: [.href("/about")], "About"))
)
)
)
)
} Now all you have to do is pass your main page's content to the |
Beta Was this translation helpful? Give feedback.
-
@mbrandonw Maybe an action item before closing this out would be to refactor the example app to have a layout. |
Beta Was this translation helpful? Give feedback.
-
Regarding components, I am trying to extend a working SwifthtmlTemplateBlog.swift to inlcude the equivalent of Resources/HtmlParts/base_header.html and Resources/HtmlParts/base_footer.html with-in the The following should be trivial, but the attempts so far only produce errors:
Also, when would one use the dot |
Beta Was this translation helpful? Give feedback.
-
@marc-medley Hey Marc, seems like we really need to improve the documentation, both for this repo and
If you go back to the let header = Node.header(
.ul(
.li(.a(attributes: [.href("/")], "Home")),
.li(.a(attributes: [.href("/contact")], "Contact us")),
.li(.a(attributes: [.href("/about")], "About"))
)
)
let footer = Node.footer(
.ul(
.li(.a(attributes: [.href("/")], "Home")),
.li(.a(attributes: [.href("/contact")], "Contact us")),
.li(.a(attributes: [.href("/about")], "About"))
)
)
func layout(mainContent: Node) -> Node {
return .html(
.head(
// whatever you want in here
),
.body(
header
.main(mainContent),
footer
)
)
} So instead of breaking out "partial" or "fragment" views into their own template files, you just create a value that holds the fragment and include it into a bigger document.
The doctype is a special case on the node enum, and can be used like so: let document: Node = [
.doctype,
.html(
.head(),
.body()
)
] By default it uses a plain let document: Node = [
.doctype(#"HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"#),
.html(
.head(),
.body()
)
]
Both the .script(attributes: [
.async(true),
.src("https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js")
])
Yeah, sorry about the confusion here, but we recently refactored |
Beta Was this translation helpful? Give feedback.
-
The ability to add components, like header and footer as separate swift files, the vapor team suggested "that a controller-like class that has methods that return Html.Node (pointfree's type), which your route handler can call, effectively, you're replacing templates with Swift functions
which are needed to be known at compile time"
Beta Was this translation helpful? Give feedback.
All reactions