Skip to content

Commit ff7b227

Browse files
committed
template: embed .tmpl
Embed template files using `go:embed`.
1 parent c3fcc59 commit ff7b227

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

template/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ limitations under the License.
2323
package main
2424

2525
import (
26+
"embed"
2627
"html/template"
2728
"log"
2829
"net/http"
@@ -35,10 +36,13 @@ func main() {
3536
log.Fatal(http.ListenAndServe("localhost:8080", nil))
3637
}
3738

39+
//go:embed index.tmpl image.tmpl
40+
var templateFS embed.FS
41+
3842
// indexTemplate is the main site template.
3943
// The default template includes two template blocks ("sidebar" and "content")
4044
// that may be replaced in templates derived from this one.
41-
var indexTemplate = template.Must(template.ParseFiles("index.tmpl"))
45+
var indexTemplate = template.Must(template.ParseFS(templateFS, "index.tmpl"))
4246

4347
// Index is a data structure used to populate an indexTemplate.
4448
type Index struct {
@@ -70,7 +74,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
7074

7175
// imageTemplate is a clone of indexTemplate that provides
7276
// alternate "sidebar" and "content" templates.
73-
var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFiles("image.tmpl"))
77+
var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFS(templateFS, "image.tmpl"))
7478

7579
// Image is a data structure used to populate an imageTemplate.
7680
type Image struct {

0 commit comments

Comments
 (0)