V
V
v- death2015-10-24 22:49:38
go
v- death, 2015-10-24 22:49:38

How to work with html/template?

Hello. Here is the function that parses the html

func Views(templateName string, p map[string]string, w http.ResponseWriter) {
  t, err := template.ParseFiles("views/"+templateName)
  if err != nil {
    w.Header().Set("Status", "404 Not Found")
  }
  w.Header().Set("Content-Type", "text/html")
  t.Execute(w, p)
}

I only learned to transfer data from the map.
I want to include head.html in the template that I load. I do so
{{template "head.html".}}
. As a result, a blank screen.
Then I can't figure out how to cram for into template.
Here is the cycle
for _, bk := range bks {
    fmt.Fprintf(w, "'%s':'%s',", bk.id, bk.name)
  }

I want to embed the data from it into the template directly, and not through my crutch.
(I load the top of the page from above. Here I generate the main content and load the bottom of the page in a loop)
If possible, give an example code.
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aaronmortum, 2015-10-26
@vGrabko99

Look at https://www.youtube.com/watch?v=k27Oga3Wmxs
There is an example of how to make a simple application

{{ define "index" }}

{{ template "header" }}

{{ range $key, $value := . }}
<div class="row">
  <div class="col-xs-2">
  </div>
  <div class="col-xs-8">
    <h1><a href="/edit?id={{$key}}">{{ $value.Title }}</a></h1>
  </div>
  <div class="col-xs-2">
  </div>
</div>
<div class="row">
  <div class="col-xs-2">
  </div>
  <div class="col-xs-8">
    {{ $value.Content }}
  </div>
  <div class="col-xs-2">
  </div>
</div>
{{ end }}

{{ template "footer" }}

{{ end }}

A
Axian Ltd., 2015-10-25
@AxianLTD

Fuck. Why try to write your own framework when there are already a bunch of others? JSF in hand forward...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question