D
D
danforth2017-02-18 21:31:55
go
danforth, 2017-02-18 21:31:55

How to correctly pass a value to a template with inheritance?

Hello! There are three files:

  • index.html
  • header.html
  • footer.html

In index.html {{template "header"}}other templates are included ( ), in header.html itself the title tag is filled like this:
<title>{{.Title}}</title>
Created in advance
type Page struct {
Title string
}

But when I do
t.ExecuteTemplate(w, "header", p) // type Page struct

then only the header is displayed in the browser, without the footer (only one template is executed), and if you do ExecuteTemplate(w, "index", Page) then everything is displayed as it should, but the Title is not filled / displayed. What could be the snag?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jezuser, 2017-02-21
@jezuser

In index.html {{ template "header" . }}
in main.go

var t *template.Template

func init() {
    t = template.Must(template.ParseFiles("index.html", "header.html", "footer.html"))
    t.ExecuteTemplate(w, "index", Page)
}

Examples of working with templates .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question