S
S
s_pyanov2016-10-15 06:28:37
go
s_pyanov, 2016-10-15 06:28:37

How to display variable values ​​on html page in go?

Good day to all. I ask you not to throw stones, but to explain on your fingers.
I am trying to write a web application in go. I connect pages, css, templates, etc. Now it is necessary to display data from variables on the html page, I try to display them in such a construction {{ .stroka }}, but nothing is displayed on the page. I guess that I don’t understand correctly how variables should be connected to the page, but I can’t figure out how to do it! tell me which way to dig?
function code

func sheduleHandler(w http.ResponseWriter, r *http.Request) {
  t, err := template.ParseFiles(
    "production/shedule.html",
    "production/header.html",
    "production/left.html",
    "production/scripts.html",
    "production/widgets.html")

  if err != nil{ 
    fmt.Println(w, err.Error())
  }
        test:="textfromgo"
  t.ExecuteTemplate(w, "shedule", nil)

}

Page code
{{ define "shedule" }}
{{ template "header" }}
<table width="99%"  border="0">
    <tr>
        <td width="230" bgcolor="#2A3F54" style="">          {{ template "left" }}   </td>
        <td style="background-color: #F7F7F7;" valign="top">
            <p>{{.test}}</p>
        </td>
    </tr>
</table>
{{ template "scr" }}
{{ end }}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2016-10-15
@s_pyanov

t.ExecuteTemplate(w, "shedule", struct{Test string}{Test: test})

And in template refer to {{.Test}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question