using templates for rendering stuff to html

This commit is contained in:
Niko Abeler 2022-07-23 18:37:41 +02:00
parent a74fd47710
commit b139b15ca6
5 changed files with 93 additions and 11 deletions

View File

@ -5,11 +5,11 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title> <title>{{ .Title }}</title>
</head> </head>
<body> <body>
{{content}} {{ .Content }}
</body> </body>
</html> </html>

2
embed/post.html Normal file
View File

@ -0,0 +1,2 @@
<h1>{{.Title}}</h1>
{{.Post}}

9
embed/user-list.html Normal file
View File

@ -0,0 +1,9 @@
{{range .Users}}
<ul>
<li>
<a href="{{ .Path() }}">
{{ .Name() }}
</a>
</li>
</ul>
{{end}}

View File

@ -1,22 +1,81 @@
package kiss package kiss
import "strings" import (
"bytes"
_ "embed"
"html/template"
)
//go:embed embed/user-list.html
var userListTemplateStr string
//go:embed embed/post.html
var postTemplateStr string
type PageContent struct {
Title string
Content template.HTML
}
type PostRenderData struct {
Title string
Post template.HTML
}
func RenderPost(post Post) (string, error) { func RenderPost(post Post) (string, error) {
template, _ := post.user.Template() baseTemplate, _ := post.user.Template()
buf, _ := post.MarkdownData() buf, _ := post.MarkdownData()
postHtml := "<h1>" + post.Title() + "</h1>\n"
postHtml += buf.String() postTemplate, _ := template.New("post").Parse(postTemplateStr)
return strings.Replace(template, "{{content}}", postHtml, -1), nil var postHtml bytes.Buffer
err := postTemplate.Execute(&postHtml, PostRenderData{
Title: post.Title(),
Post: template.HTML(buf.String()),
})
if err != nil {
return "", err
}
data := PageContent{
Title: post.Title(),
Content: template.HTML(postHtml.String()),
}
var html bytes.Buffer
t, err := template.New("page").Parse(baseTemplate)
t.Execute(&html, data)
return html.String(), err
} }
func RenderIndexPage(user User) (string, error) { func RenderIndexPage(user User) (string, error) {
template, _ := user.Template() baseTemplate, _ := user.Template()
posts, _ := user.Posts() posts, _ := user.Posts()
postHtml := "" postHtml := ""
for _, postId := range posts { for _, postId := range posts {
post, _ := user.GetPost(postId) post, _ := user.GetPost(postId)
postHtml += "<h2><a href=\"" + post.Path() + "\">" + post.Title() + "</a></h2>\n" postHtml += "<h2><a href=\"" + post.Path() + "\">" + post.Title() + "</a></h2>\n"
} }
return strings.Replace(template, "{{content}}", postHtml, -1), nil
data := PageContent{
Title: "Index",
Content: template.HTML(postHtml),
}
var html bytes.Buffer
t, err := template.New("post").Parse(baseTemplate)
t.Execute(&html, data)
return html.String(), err
} }
// func RenderUserList(user User) (string, error) {
// base_template, _ := user.Template()
// users, _ := user.repo.Users()
// template.New("user_list").Parse()
// return strings.Replace(template, "{{content}}", userHtml, -1), nil
// }

View File

@ -9,7 +9,13 @@ import (
func TestCanRenderPost(t *testing.T) { func TestCanRenderPost(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
result, _ := kiss.RenderPost(post) result, err := kiss.RenderPost(post)
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
if !strings.Contains(result, "<h1>testpost</h1>") { if !strings.Contains(result, "<h1>testpost</h1>") {
t.Error("Post title not rendered as h1. Got: " + result) t.Error("Post title not rendered as h1. Got: " + result)
} }
@ -19,7 +25,13 @@ func TestCanRenderPost(t *testing.T) {
func TestRendererUsesBaseTemplate(t *testing.T) { func TestRendererUsesBaseTemplate(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
result, _ := kiss.RenderPost(post) result, err := kiss.RenderPost(post)
if err != nil {
t.Error("Error rendering post: " + err.Error())
return
}
if !strings.Contains(result, "<html") { if !strings.Contains(result, "<html") {
t.Error("Base template not used. Got: " + result) t.Error("Base template not used. Got: " + result)
} }