Convert search page to Templ
This commit is contained in:
parent
1e3ee29802
commit
6d72f5ad9b
@ -4,10 +4,13 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
||||
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
|
||||
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
|
||||
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
|
||||
"git.grosinger.net/tgrosinger/saasitone/templates"
|
||||
)
|
||||
|
||||
@ -17,11 +20,6 @@ type (
|
||||
Search struct {
|
||||
*services.TemplateRenderer
|
||||
}
|
||||
|
||||
searchResult struct {
|
||||
Title string
|
||||
URL string
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -39,23 +37,28 @@ func (h *Search) Routes(g *echo.Group) {
|
||||
|
||||
func (h *Search) Page(ctx echo.Context) error {
|
||||
p := page.New(ctx)
|
||||
p.Layout = templates.LayoutMain
|
||||
p.Name = templates.PageSearch
|
||||
|
||||
// Fake search results
|
||||
var results []searchResult
|
||||
var results []pages.SearchResult
|
||||
if search := ctx.QueryParam("query"); search != "" {
|
||||
for i := 0; i < 5; i++ {
|
||||
title := "Lorem ipsum example ddolor sit amet"
|
||||
index := rand.Intn(len(title))
|
||||
title = title[:index] + search + title[index:]
|
||||
results = append(results, searchResult{
|
||||
results = append(results, pages.SearchResult{
|
||||
Title: title,
|
||||
URL: fmt.Sprintf("https://www.%s.com", search),
|
||||
})
|
||||
}
|
||||
}
|
||||
p.Data = results
|
||||
|
||||
return h.RenderPage(ctx, p)
|
||||
component := pages.Search(p, results)
|
||||
|
||||
// TODO: This can be reused
|
||||
p.LayoutComponent = func(content templ.Component) templ.Component {
|
||||
return layouts.Main(p, content)
|
||||
}
|
||||
|
||||
return h.RenderPageTempl(ctx, p, component)
|
||||
}
|
||||
|
14
templ/pages/search.templ
Normal file
14
templ/pages/search.templ
Normal file
@ -0,0 +1,14 @@
|
||||
package pages
|
||||
|
||||
import "git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
||||
|
||||
type SearchResult struct {
|
||||
Title string
|
||||
URL string
|
||||
}
|
||||
|
||||
templ Search(p page.Page, results []SearchResult) {
|
||||
for _, result := range results {
|
||||
<a class="panel-block" href={ templ.URL(result.URL) }>{ result.Title }</a>
|
||||
}
|
||||
}
|
66
templ/pages/search_templ.go
Normal file
66
templ/pages/search_templ.go
Normal file
@ -0,0 +1,66 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.2.707
|
||||
package pages
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import "context"
|
||||
import "io"
|
||||
import "bytes"
|
||||
|
||||
import "git.grosinger.net/tgrosinger/saasitone/pkg/page"
|
||||
|
||||
type SearchResult struct {
|
||||
Title string
|
||||
URL string
|
||||
}
|
||||
|
||||
func Search(p page.Page, results []SearchResult) templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
templ_7745c5c3_Buffer = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
for _, result := range results {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a class=\"panel-block\" href=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 templ.SafeURL = templ.URL(result.URL)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
|
||||
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
|
||||
}
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(result.Title)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/pages/search.templ`, Line: 12, Col: 70}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
|
||||
}
|
||||
return templ_7745c5c3_Err
|
||||
})
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
{{define "content"}}
|
||||
{{- range .Data}}
|
||||
<a class="panel-block" href="{{.URL}}">{{.Title}}</a>
|
||||
{{- end}}
|
||||
{{end}}
|
Loading…
Reference in New Issue
Block a user