v2 #43

Merged
h4kor merged 41 commits from v2 into main 2023-07-19 19:09:19 +00:00
17 changed files with 138 additions and 84 deletions
Showing only changes of commit f513205cc3 - Show all commits

View File

@ -1,5 +1,10 @@
package model package model
import (
"fmt"
"owl-blogs/render"
)
type Article struct { type Article struct {
EntryBase EntryBase
meta ArticleMetaData meta ArticleMetaData
@ -10,8 +15,16 @@ type ArticleMetaData struct {
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
func (e *Article) Title() string {
return e.meta.Title
}
func (e *Article) Content() EntryContent { func (e *Article) Content() EntryContent {
return EntryContent(e.meta.Content) str, err := render.RenderTemplateToString("entry/Article", e)
if err != nil {
fmt.Println(err)
}
return EntryContent(str)
} }
func (e *Article) MetaData() interface{} { func (e *Article) MetaData() interface{} {

View File

@ -9,7 +9,9 @@ type Entry interface {
Content() EntryContent Content() EntryContent
PublishedAt() *time.Time PublishedAt() *time.Time
MetaData() interface{} MetaData() interface{}
// Create(id string, publishedAt *time.Time, metaData EntryMetaData) error
// Optional: can return empty string
Title() string
SetID(id string) SetID(id string)
SetPublishedAt(publishedAt *time.Time) SetPublishedAt(publishedAt *time.Time)

View File

@ -1,5 +1,10 @@
package model package model
import (
"fmt"
"owl-blogs/render"
)
type Image struct { type Image struct {
EntryBase EntryBase
meta ImageMetaData meta ImageMetaData
@ -10,8 +15,16 @@ type ImageMetaData struct {
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
func (e *Image) Title() string {
return ""
}
func (e *Image) Content() EntryContent { func (e *Image) Content() EntryContent {
return EntryContent(e.meta.Content) str, err := render.RenderTemplateToString("entry/Image", e)
if err != nil {
fmt.Println(err)
}
return EntryContent(str)
} }
func (e *Image) MetaData() interface{} { func (e *Image) MetaData() interface{} {

52
render/templates.go Normal file
View File

@ -0,0 +1,52 @@
package render
import (
"bytes"
"embed"
"io"
"text/template"
)
//go:embed templates
var templates embed.FS
func CreateTemplateWithBase(templateName string) (*template.Template, error) {
return template.ParseFS(
templates,
"templates/base.tmpl",
"templates/"+templateName+".tmpl",
)
}
func RenderTemplateWithBase(w io.Writer, templateName string, data interface{}) error {
t, err := CreateTemplateWithBase(templateName)
if err != nil {
return err
}
err = t.ExecuteTemplate(w, "base", data)
return err
}
func RenderTemplateToString(templateName string, data interface{}) (string, error) {
t, err := template.ParseFS(
templates,
"templates/"+templateName+".tmpl",
)
if err != nil {
return "", err
}
var output bytes.Buffer
err = t.Execute(&output, data)
return output.String(), err
}

View File

@ -0,0 +1 @@
{{.MetaData.Content}}

View File

@ -0,0 +1,8 @@
<h1>
{{.MetaData.Title}}
</h1>
<img src="/media/{{.MetaData.ImageId}}">
{{.MetaData.Content}}

View File

@ -1,17 +1,18 @@
{{define "title"}}{{.MetaData.Title}}{{end}} {{define "title"}}{{.Title}}{{end}}
{{define "main"}} {{define "main"}}
<h1>{{.MetaData.Title}}</h1> {{if .Title}}
<h1>{{.Title}}</h1>
<p> {{end}}
{{.Content}}
</p>
<p> <p>
Published: {{.PublishedAt}} Published: {{.PublishedAt}}
</p> </p>
{{.Content}}
{{end}} {{end}}

View File

@ -0,0 +1,22 @@
{{define "title"}}Index{{end}}
{{define "main"}}
{{ range . }}
<div>
<h2>
<a href="/posts/{{ .ID }}">
{{if .Title}}
{{ .Title }}
{{else}}
#
{{end}}
</a>
</h2>
<p>{{ .PublishedAt }}</p>
{{ .Content }}
</div>
<hr>
{{ end }}
{{end}}

View File

@ -2,6 +2,7 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/render"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -32,5 +33,5 @@ func (h *EditorListHandler) Handle(c *fiber.Ctx) error {
typeNames = append(typeNames, name) typeNames = append(typeNames, name)
} }
return RenderTemplate(c, "views/editor_list", &EditorListContext{Types: typeNames}) return render.RenderTemplateWithBase(c, "views/editor_list", &EditorListContext{Types: typeNames})
} }

View File

@ -2,8 +2,7 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/domain/model" "owl-blogs/render"
"text/template"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -17,18 +16,6 @@ func NewEntryHandler(entryService *app.EntryService, registry *app.EntryTypeRegi
return &EntryHandler{entrySvc: entryService, registry: registry} return &EntryHandler{entrySvc: entryService, registry: registry}
} }
func (h *EntryHandler) getTemplate(entry model.Entry) (*template.Template, error) {
name, err := h.registry.TypeName(entry)
if err != nil {
return nil, err
}
return template.ParseFS(
templates,
"templates/base.tmpl",
"templates/views/entry/"+name+".tmpl",
)
}
func (h *EntryHandler) Handle(c *fiber.Ctx) error { func (h *EntryHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
@ -38,10 +25,5 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
return err return err
} }
template, err := h.getTemplate(entry) return render.RenderTemplateWithBase(c, "views/entry", entry)
if err != nil {
return err
}
return template.ExecuteTemplate(c, "base", entry)
} }

View File

@ -2,6 +2,7 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/render"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -15,5 +16,13 @@ func NewIndexHandler(entryService *app.EntryService) *IndexHandler {
} }
func (h *IndexHandler) Handle(c *fiber.Ctx) error { func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!") c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
entries, err := h.entrySvc.FindAll()
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, "views/index", entries)
} }

View File

@ -2,6 +2,7 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/render"
"time" "time"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -17,7 +18,7 @@ func NewLoginHandler(authorService *app.AuthorService) *LoginHandler {
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error { func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return RenderTemplate(c, "views/login", nil) return render.RenderTemplateWithBase(c, "views/login", nil)
} }
func (h *LoginHandler) HandlePost(c *fiber.Ctx) error { func (h *LoginHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -1,34 +0,0 @@
package web
import (
"embed"
"io"
"text/template"
)
//go:embed templates
var templates embed.FS
func CreateTemplate(templateName string) (*template.Template, error) {
return template.ParseFS(
templates,
"templates/base.tmpl",
"templates/"+templateName+".tmpl",
)
}
func RenderTemplate(w io.Writer, templateName string, data interface{}) error {
t, err := CreateTemplate(templateName)
if err != nil {
return err
}
err = t.ExecuteTemplate(w, "base", data)
return err
}

View File

@ -1,17 +0,0 @@
{{define "title"}}Image Entry{{end}}
{{define "main"}}
<img src="/media/{{.MetaData.ImageId}}">
<p>
{{.Content}}
</p>
<p>
Published: {{.PublishedAt}}
</p>
{{end}}