fix context
This commit is contained in:
parent
7cbefe137a
commit
533d5b173f
|
@ -35,7 +35,7 @@ func (c *Context) hfShort(id int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) hfRelated(id, to 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 {
|
if c.HfId == id {
|
||||||
return c.hfShort(id)
|
return c.hfShort(id)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -24,7 +24,10 @@ type EventList struct {
|
||||||
|
|
||||||
func NewEventList(world *DfWorld, obj any) *EventList {
|
func NewEventList(world *DfWorld, obj any) *EventList {
|
||||||
el := EventList{
|
el := EventList{
|
||||||
Context: &Context{HfId: -1},
|
Context: &Context{
|
||||||
|
World: world,
|
||||||
|
HfId: -1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
switch x := obj.(type) {
|
switch x := obj.(type) {
|
||||||
|
|
|
@ -7,8 +7,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
|
|
||||||
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parms map[string]string
|
type Parms map[string]string
|
||||||
|
@ -32,12 +30,7 @@ func (srv *DfServer) RegisterWorldPage(path string, template string, accessor fu
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
td := &templates.TemplateData{
|
err := srv.templates.Render(w, template, accessor(mux.Vars(r)))
|
||||||
Context: &model.Context{World: srv.context.world},
|
|
||||||
Data: accessor(mux.Vars(r)),
|
|
||||||
}
|
|
||||||
|
|
||||||
err := srv.templates.Render(w, template, td)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(w, err)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
@ -72,7 +72,6 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// }
|
// }
|
||||||
// prepend the path with the path to the static directory
|
// prepend the path with the path to the static directory
|
||||||
path = h.staticPath + path
|
path = h.staticPath + path
|
||||||
fmt.Println(r.URL, "->", path)
|
|
||||||
|
|
||||||
_, err := h.staticFS.Open(path)
|
_, err := h.staticFS.Open(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
@ -125,7 +124,7 @@ func (h loadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println(err)
|
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 {
|
if err != nil {
|
||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(w, err)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
@ -29,7 +29,6 @@ func (srv *DfServer) LoadTemplates() {
|
||||||
"region": func(id int) template.HTML { return model.LinkRegion(srv.context.world, id) },
|
"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] },
|
"getRegion": func(id int) *model.Region { return srv.context.world.Regions[id] },
|
||||||
"events": func(obj any) *model.EventList {
|
"events": func(obj any) *model.EventList {
|
||||||
fmt.Println("W", srv.context.world)
|
|
||||||
return model.NewEventList(srv.context.world, obj)
|
return model.NewEventList(srv.context.world, obj)
|
||||||
},
|
},
|
||||||
"season": model.Season,
|
"season": model.Season,
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
Eventss
|
|
||||||
{{ json .Context }}
|
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
{{- range $event := .Data.Events }}
|
{{- range $event := .Events }}
|
||||||
<li>
|
<li>
|
||||||
[{{ $event.Id }}] In {{ if gt $event.Seconds72 -1 }}{{ season $event.Seconds72 }} of {{ end }}{{ $event.Year }}, {{
|
[{{ $event.Id }}] In {{ if gt $event.Seconds72 -1 }}{{ season $event.Seconds72 }} of {{ end }}{{ $event.Year }}, {{
|
||||||
html ($event.Details.Html $.Context) }} {{ json $event.Details }}
|
html ($event.Details.Html $.Context) }} {{ json $event.Details }}
|
||||||
|
|
|
@ -2,11 +2,8 @@ package templates
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed *.html
|
//go:embed *.html
|
||||||
|
@ -17,11 +14,6 @@ type Template struct {
|
||||||
templates *template.Template
|
templates *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
type TemplateData struct {
|
|
||||||
Context *model.Context
|
|
||||||
Data any
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(funcMap template.FuncMap) *Template {
|
func New(funcMap template.FuncMap) *Template {
|
||||||
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(templateFS, "*.html"))
|
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(templateFS, "*.html"))
|
||||||
return &Template{
|
return &Template{
|
||||||
|
@ -40,9 +32,8 @@ func NewDebug(funcMap template.FuncMap) *Template {
|
||||||
|
|
||||||
var DebugTemplates = false
|
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 {
|
if DebugTemplates {
|
||||||
fmt.Println("RENDER", name)
|
|
||||||
tmpl := NewDebug(t.funcMap).templates
|
tmpl := NewDebug(t.funcMap).templates
|
||||||
tmpl = template.Must(tmpl.ParseFiles("templates/" + name))
|
tmpl = template.Must(tmpl.ParseFiles("templates/" + name))
|
||||||
return tmpl.ExecuteTemplate(w, name, data)
|
return tmpl.ExecuteTemplate(w, name, data)
|
||||||
|
|
Loading…
Reference in New Issue