diff --git a/embed/base.html b/embed/base.html index 697c8b1..20cab4b 100644 --- a/embed/base.html +++ b/embed/base.html @@ -5,11 +5,11 @@ - {{title}} + {{ .Title }} - {{content}} + {{ .Content }} \ No newline at end of file diff --git a/embed/post.html b/embed/post.html new file mode 100644 index 0000000..19a2483 --- /dev/null +++ b/embed/post.html @@ -0,0 +1,2 @@ +

{{.Title}}

+{{.Post}} \ No newline at end of file diff --git a/embed/user-list.html b/embed/user-list.html new file mode 100644 index 0000000..fe33eac --- /dev/null +++ b/embed/user-list.html @@ -0,0 +1,9 @@ +{{range .Users}} + +{{end}} \ No newline at end of file diff --git a/renderer.go b/renderer.go index 7a40820..e2f41a0 100644 --- a/renderer.go +++ b/renderer.go @@ -1,22 +1,81 @@ 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) { - template, _ := post.user.Template() + baseTemplate, _ := post.user.Template() buf, _ := post.MarkdownData() - postHtml := "

" + post.Title() + "

\n" - postHtml += buf.String() - return strings.Replace(template, "{{content}}", postHtml, -1), nil + + postTemplate, _ := template.New("post").Parse(postTemplateStr) + 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) { - template, _ := user.Template() + baseTemplate, _ := user.Template() posts, _ := user.Posts() + postHtml := "" for _, postId := range posts { post, _ := user.GetPost(postId) postHtml += "

" + post.Title() + "

\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 +// } diff --git a/renderer_test.go b/renderer_test.go index 01c19d0..ee454c9 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -9,7 +9,13 @@ import ( func TestCanRenderPost(t *testing.T) { user := getTestUser() 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, "

testpost

") { t.Error("Post title not rendered as h1. Got: " + result) } @@ -19,7 +25,13 @@ func TestCanRenderPost(t *testing.T) { func TestRendererUsesBaseTemplate(t *testing.T) { user := getTestUser() 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, "