From 895c7905f9e39bbb046c130a3a94e4044bf6c989 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Mon, 7 Oct 2024 04:18:59 +0300 Subject: Подгрузка рандома аяксом MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/handler/random.go | 15 ++++++++++++++- pkg/tpl/layout.templ | 1 + pkg/tpl/layout_templ.go | 2 +- pkg/tpl/random.templ | 18 ++++++------------ pkg/tpl/random_templ.go | 45 +++++++-------------------------------------- 5 files changed, 29 insertions(+), 52 deletions(-) (limited to 'pkg') diff --git a/pkg/handler/random.go b/pkg/handler/random.go index 091058f..29c5f6f 100644 --- a/pkg/handler/random.go +++ b/pkg/handler/random.go @@ -1,6 +1,7 @@ package handler import ( + "github.com/a-h/templ" "github.com/labstack/echo/v4" "sh.org.ru/pkg/model" "sh.org.ru/pkg/tpl" @@ -14,5 +15,17 @@ func (h *Handler) Random(c echo.Context) error { return err } - return tpl.Random(quotes).Render(c.Request().Context(), c.Response()) + comp := tpl.Random(quotes) + + if c.Request().Header.Get("Hx-Request") == "true" { + return comp.Render(c.Request().Context(), c.Response()) + } + + ctx := templ.WithChildren(c.Request().Context(), comp) + + return tpl.Layout(tpl.HeaderParams{ + Title: "Цитатник Рунета", + Description: "Новый цитатник Рунета", + URL: "https://sh.org.ru/", + }).Render(ctx, c.Response()) } diff --git a/pkg/tpl/layout.templ b/pkg/tpl/layout.templ index 2c41a0d..c777457 100644 --- a/pkg/tpl/layout.templ +++ b/pkg/tpl/layout.templ @@ -42,6 +42,7 @@ templ Layout(params HeaderParams) { + } diff --git a/pkg/tpl/layout_templ.go b/pkg/tpl/layout_templ.go index eb340ee..a86bb30 100644 --- a/pkg/tpl/layout_templ.go +++ b/pkg/tpl/layout_templ.go @@ -76,7 +76,7 @@ func Layout(params HeaderParams) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/pkg/tpl/random.templ b/pkg/tpl/random.templ index 63e7c03..39ce3ff 100644 --- a/pkg/tpl/random.templ +++ b/pkg/tpl/random.templ @@ -1,18 +1,12 @@ package tpl -import "sh.org.ru/pkg/model" -import "math/rand" -import "fmt" +import ( + "sh.org.ru/pkg/model" +) templ Random(quotes []model.Quote) { - @Layout(HeaderParams{ - Title: "Цитатник Рунета -- случайные", - Description: "Новый цитатник Рунета", - URL: "https://sh.org.ru/random", - }) { - for _, q := range quotes { - @Quote(&q) - } - Обновить + for _, q := range quotes { + @Quote(&q) } + Загрузить ещё... } diff --git a/pkg/tpl/random_templ.go b/pkg/tpl/random_templ.go index 19a7035..79f5685 100644 --- a/pkg/tpl/random_templ.go +++ b/pkg/tpl/random_templ.go @@ -8,9 +8,9 @@ package tpl import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -import "sh.org.ru/pkg/model" -import "math/rand" -import "fmt" +import ( + "sh.org.ru/pkg/model" +) func Random(quotes []model.Quote) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { @@ -33,44 +33,13 @@ func Random(quotes []model.Quote) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { - templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) - if !templ_7745c5c3_IsBuffer { - defer func() { - templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) - if templ_7745c5c3_Err == nil { - templ_7745c5c3_Err = templ_7745c5c3_BufErr - } - }() - } - ctx = templ.InitializeContext(ctx) - for _, q := range quotes { - templ_7745c5c3_Err = Quote(&q).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" Обновить") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - return templ_7745c5c3_Err - }) - templ_7745c5c3_Err = Layout(HeaderParams{ - Title: "Цитатник Рунета -- случайные", - Description: "Новый цитатник Рунета", - URL: "https://sh.org.ru/random", - }).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Загрузить ещё...") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } -- cgit v1.2.3