-
Notifications
You must be signed in to change notification settings - Fork 0
Set data to template
Sergei edited this page Mar 16, 2015
·
1 revision
The last parameter of methods Render and RenderView is data, which will be available in the template. To add data to template you have to use struct:
ctrl.Render(res, "template",nil, struct {
Numbers [5]int
Article string
Data int
}{
Numbers: [5]int{1, 14, 44, 15, 29},
Article: "Make a join",
Data: 15,
})
As you can see, we have declared a struct and put the values immediately. These values now are available in the template
{{define "body"}}
{{ $data := .Data }}
<h1>{{.Data.Article}}</h1>
<ul>
{{ range $number := .Data.Numbers}}
<li><a href="javascript:void(0)">{{$number}}</a></li>
{{end}}
</ul>
<h2>{{ $data.Data }}</h2>
{{ template "ajax" .Data}}
{{end}}
The same for method RenderView:
ctrl.RenderView(res, "post", nil, struct {
User string
Dates [2]int
}{
User: "John",
Dates: [2]int{2, 3},
})
<html>
<head>
<title>Posts</title>
</head>
<body>
<h1>Posts!</h1>
<p>User name: {{.Data.User}}</p>
<p>Your dates pleas:</p>
{{range $date := .Data.Dates}}
<p><i>{{$date}}</i></p>
{{end}}
</body>
</html>