Skip to content

Commit e7f2234

Browse files
committed
template: add example to demonstrate template blocks
Fixes golang/go#14285 Change-Id: I3b32b7fc19960a77778e84cba2a0a95b49dbdf16 Reviewed-on: https://go-review.googlesource.com/19443 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Chris Broadfoot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent ff9d95a commit e7f2234

4 files changed

Lines changed: 152 additions & 0 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ A trivial "Hello, world" App Engine application intended to be used as the
5050
starting point for your own code.
5151

5252
_Note_: The `goapp` tool is part of the [Google App Engine SDK for Go](https://cloud.google.com/appengine/downloads#Google_App_Engine_SDK_for_Go).
53+
54+
### [template][template/] ([godoc](//godoc.org/github.com/golang/example/template))
55+
56+
A trivial web server that demonstrates the use of the
57+
[`template` package](https://golang.org/pkg/text/template/)'s `block` feature.

template/image.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{define "content"}}
2+
<div id="image">
3+
<img src="{{.URL}}" title="{{.Title}}" alt="{{.Title}}">
4+
</div>
5+
{{end}}
6+
7+
{{define "sidebar"}}
8+
<a href="/">Back</a>
9+
{{end}}

template/index.tmpl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>{{.Title}}</title>
6+
<style>
7+
body {
8+
font-family: sans-serif;
9+
}
10+
h1 {
11+
background: #ddd;
12+
}
13+
#sidebar {
14+
float: right;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<h1>{{.Title}}</h1>
20+
21+
<div id="sidebar">
22+
{{block "sidebar" .}}
23+
<h2>Links</h2>
24+
{{/* The dashes in the following template directives
25+
ensure the generated HTML of this list contains no
26+
extraneous spaces or line breaks. */}}
27+
<ul id="sidebar">
28+
{{- range .Links}}
29+
<li><a href="{{.URL}}">{{.Title}}</a></li>
30+
{{- end}}
31+
</ul>
32+
{{end}}
33+
</div>
34+
35+
{{block "content" .}}
36+
<div id="content">
37+
{{.Body}}
38+
</div>
39+
{{end}}
40+
</body>
41+
</html>

template/main.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2016 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Command template is a trivial web server that uses the text/template (and
18+
// html/template) package's "block" feature to implement a kind of template
19+
// inheritance.
20+
//
21+
// It should be executed from the directory in which the source resides,
22+
// as it will look for its template files in the current directory.
23+
package main
24+
25+
import (
26+
"html/template"
27+
"log"
28+
"net/http"
29+
"strings"
30+
)
31+
32+
func main() {
33+
http.HandleFunc("/", indexHandler)
34+
http.HandleFunc("/image/", imageHandler)
35+
log.Fatal(http.ListenAndServe("localhost:8080", nil))
36+
}
37+
38+
// indexTemplate is the main site template.
39+
// The default template includes two template blocks ("sidebar" and "content")
40+
// that may be replaced in templates derived from this one.
41+
var indexTemplate = template.Must(template.ParseFiles("index.tmpl"))
42+
43+
// Index is a data structure used to populate an indexTemplate.
44+
type Index struct {
45+
Title string
46+
Body string
47+
Links []Link
48+
}
49+
50+
type Link struct {
51+
URL, Title string
52+
}
53+
54+
// indexHandler is an HTTP handler that serves the index page.
55+
func indexHandler(w http.ResponseWriter, r *http.Request) {
56+
data := &Index{
57+
Title: "Image gallery",
58+
Body: "Welcome to the image gallery.",
59+
}
60+
for name, img := range images {
61+
data.Links = append(data.Links, Link{
62+
URL: "/image/" + name,
63+
Title: img.Title,
64+
})
65+
}
66+
if err := indexTemplate.Execute(w, data); err != nil {
67+
log.Println(err)
68+
}
69+
}
70+
71+
// imageTemplate is a clone of indexTemplate that provides
72+
// alternate "sidebar" and "content" templates.
73+
var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFiles("image.tmpl"))
74+
75+
// Image is a data structure used to populate an imageTemplate.
76+
type Image struct {
77+
Title string
78+
URL string
79+
}
80+
81+
// imageHandler is an HTTP handler that serves the image pages.
82+
func imageHandler(w http.ResponseWriter, r *http.Request) {
83+
data, ok := images[strings.TrimPrefix(r.URL.Path, "/image/")]
84+
if !ok {
85+
http.NotFound(w, r)
86+
return
87+
}
88+
if err := imageTemplate.Execute(w, data); err != nil {
89+
log.Println(err)
90+
}
91+
}
92+
93+
// images specifies the site content: a collection of images.
94+
var images = map[string]*Image{
95+
"go": {"The Go Gopher", "https://golang.org/doc/gopher/frontpage.png"},
96+
"google": {"The Google Logo", "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"},
97+
}

0 commit comments

Comments
 (0)