learnlytics-go/templ/examples/blog/posts.templ

65 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2025-03-20 12:35:13 +01:00
package main
import (
"fmt"
"time"
)
templ headerTemplate(name string) {
<header data-testid="headerTemplate">
<h1>{ name }</h1>
</header>
}
templ footerTemplate() {
<footer data-testid="footerTemplate">
<div>&copy; { fmt.Sprintf("%d", time.Now().Year()) }</div>
</footer>
}
templ navTemplate() {
<nav data-testid="navTemplate">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/posts">Posts</a></li>
</ul>
</nav>
}
templ layout(name string) {
<html>
<head><title>{ name }</title></head>
<body>
@headerTemplate(name)
@navTemplate()
<main>
{ children... }
</main>
</body>
@footerTemplate()
</html>
}
templ postsTemplate(posts []Post) {
<div data-testid="postsTemplate">
for _, p := range posts {
<div data-testid="postsTemplatePost">
<div data-testid="postsTemplatePostName">{ p.Name }</div>
<div data-testid="postsTemplatePostAuthor">{ p.Author }</div>
</div>
}
</div>
}
templ home() {
@layout("Home") {
<div data-testid="homeTemplate">Welcome to my website.</div>
}
}
templ posts(posts []Post) {
@layout("Posts") {
@postsTemplate(posts)
}
}