From 55448bc974f71f83e6a6c6d70afe9cfc63d275f4 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Tue, 18 Jul 2023 23:20:15 +0200 Subject: feat: Move media from `html/template` to qtpl --- pkg/view/media.go | 25 ++++++++++--------------- templates/base.qtpl | 47 +++++++++++++++++++++++++++++++++++++++++++++++ templates/media.qtpl | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 templates/base.qtpl create mode 100644 templates/media.qtpl diff --git a/pkg/view/media.go b/pkg/view/media.go index 6d380e2..4bf8d68 100644 --- a/pkg/view/media.go +++ b/pkg/view/media.go @@ -5,9 +5,9 @@ import ( "github.com/valyala/fasthttp" - "git.sr.ht/~gabrielgio/img" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" + "git.sr.ht/~gabrielgio/img/templates" ) type ( @@ -15,11 +15,6 @@ type ( mediaRepository repository.MediaRepository userRepository repository.UserRepository } - - Page struct { - Medias []*repository.Media - Next *repository.Pagination - } ) func getPagination(ctx *fasthttp.RequestCtx) *repository.Pagination { @@ -77,16 +72,16 @@ func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { return err } - err = img.Render(ctx, "media.html", &img.HTMLView[*Page]{ - Title: "Media", - Data: &Page{ - Medias: medias, - Next: &repository.Pagination{ - Size: p.Size, - Page: p.Page + 1, - }, + page := &templates.MediaPage{ + Medias: medias, + Next: &repository.Pagination{ + Size: p.Size, + Page: p.Page + 1, }, - }) + } + + templates.WritePageTemplate(ctx, page) + if err != nil { return err } diff --git a/templates/base.qtpl b/templates/base.qtpl new file mode 100644 index 0000000..cbde355 --- /dev/null +++ b/templates/base.qtpl @@ -0,0 +1,47 @@ +This is a base page template. All the other template pages implement this interface. + +{% interface +Page { + Title() + Content() + Script() +} +%} + + +Page prints a page implementing Page interface. +{% func PageTemplate(p Page) %} + + + + img | {%= p.Title() %} + + + + + + +
+ {%= p.Content() %} +
+ + {%= p.Script() %} + +{% endfunc %} + +{% code type BasePage struct {} %} +{% func (p *BasePage) Title() %}Empty{% endfunc %} +{% func (p *BasePage) Body() %}HelloWorld{% endfunc %} +{% func (p *BasePage) Script() %}{% endfunc %} diff --git a/templates/media.qtpl b/templates/media.qtpl new file mode 100644 index 0000000..88ce582 --- /dev/null +++ b/templates/media.qtpl @@ -0,0 +1,37 @@ +{% import "git.sr.ht/~gabrielgio/img/pkg/database/repository" %} + +{% code +type MediaPage struct { + Medias []*repository.Media + Next *repository.Pagination +} +%} + +{% func (p *MediaPage) Title() %} +Media +{% endfunc %} + + +{% func (p *MediaPage) Content() %} +
+{% for _, media := range p.Medias %} +
+ {% if media.IsVideo() %} + + {% else %} +
+ +
+ {% endif %} +
+{% endfor %} +
+
+ next +
+{% endfunc %} + +{% func (p *MediaPage) Script() %} +{% endfunc %} -- cgit v1.2.3