fix context

This commit is contained in:
Robert Janetzko 2022-04-26 18:06:21 +00:00
parent 7cbefe137a
commit 533d5b173f
7 changed files with 9 additions and 27 deletions

View file

@ -35,7 +35,7 @@ func (c *Context) hfShort(id int) string {
}
func (c *Context) hfRelated(id, to int) string {
if c.HfId != -1 {
if c.HfId != -1 && to != c.HfId {
if c.HfId == id {
return c.hfShort(id)
} else {

View file

@ -24,7 +24,10 @@ type EventList struct {
func NewEventList(world *DfWorld, obj any) *EventList {
el := EventList{
Context: &Context{HfId: -1},
Context: &Context{
World: world,
HfId: -1,
},
}
switch x := obj.(type) {

View file

@ -7,8 +7,6 @@ import (
"strconv"
"github.com/gorilla/mux"
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
)
type Parms map[string]string
@ -32,12 +30,7 @@ func (srv *DfServer) RegisterWorldPage(path string, template string, accessor fu
return
}
td := &templates.TemplateData{
Context: &model.Context{World: srv.context.world},
Data: accessor(mux.Vars(r)),
}
err := srv.templates.Render(w, template, td)
err := srv.templates.Render(w, template, accessor(mux.Vars(r)))
if err != nil {
fmt.Fprintln(w, err)
fmt.Println(err)

View file

@ -72,7 +72,6 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// }
// prepend the path with the path to the static directory
path = h.staticPath + path
fmt.Println(r.URL, "->", path)
_, err := h.staticFS.Open(path)
if os.IsNotExist(err) {
@ -125,7 +124,7 @@ func (h loadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println(err)
}
err = h.server.templates.Render(w, "load.html", &templates.TemplateData{Data: p})
err = h.server.templates.Render(w, "load.html", p)
if err != nil {
fmt.Fprintln(w, err)
fmt.Println(err)

View file

@ -29,7 +29,6 @@ func (srv *DfServer) LoadTemplates() {
"region": func(id int) template.HTML { return model.LinkRegion(srv.context.world, id) },
"getRegion": func(id int) *model.Region { return srv.context.world.Regions[id] },
"events": func(obj any) *model.EventList {
fmt.Println("W", srv.context.world)
return model.NewEventList(srv.context.world, obj)
},
"season": model.Season,

View file

@ -1,8 +1,5 @@
Eventss
{{ json .Context }}
<ul>
{{- range $event := .Data.Events }}
{{- range $event := .Events }}
<li>
[{{ $event.Id }}] In {{ if gt $event.Seconds72 -1 }}{{ season $event.Seconds72 }} of {{ end }}{{ $event.Year }}, {{
html ($event.Details.Html $.Context) }} {{ json $event.Details }}

View file

@ -2,11 +2,8 @@ package templates
import (
"embed"
"fmt"
"html/template"
"io"
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
)
//go:embed *.html
@ -17,11 +14,6 @@ type Template struct {
templates *template.Template
}
type TemplateData struct {
Context *model.Context
Data any
}
func New(funcMap template.FuncMap) *Template {
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(templateFS, "*.html"))
return &Template{
@ -40,9 +32,8 @@ func NewDebug(funcMap template.FuncMap) *Template {
var DebugTemplates = false
func (t *Template) Render(w io.Writer, name string, data *TemplateData) error {
func (t *Template) Render(w io.Writer, name string, data any) error {
if DebugTemplates {
fmt.Println("RENDER", name)
tmpl := NewDebug(t.funcMap).templates
tmpl = template.Must(tmpl.ParseFiles("templates/" + name))
return tmpl.ExecuteTemplate(w, name, data)