The template package
The template is a big package that can be really easy and fun to use. You have to create a way for the template package to know what files to choose. The type used for the template package is the type template.
I would explain the parse type but even the docs say it is for internal data structures only.
So the general use of the template type is the following.
The pattern is the directory the files are located. For instance “templates/*”.
The actual loading of the file you would normally put into the handler function to execute the file.
So far we have a server and a handler to serve the files. The next is to serve some data in the template. This is done with specific syntax. You would allow data by adding it to the ExecuteTemplate last param.
A usual way of utilizing the template system is using headers and footers.
You do this by adding the following to the html file {{define “header”}}
and {{end}} at the end. The same would go for the footer. This is in a sense creating data in the templates themselves.
The following would be the header.html file and the footer would end up the same way.
{{define “header”}}
<head> //scripts</head><body>{{end}}
In the files you are going to use just for the body you would setup like the following.
{{template “header”}}//just add content here{{template “footer”}}
This is the usual way to see templating in action. To pass data structures you would just add the struct with loaded data in the handler param.
The ExecutiveTemplate() is what executes the file and param.
The {{.}} Basically takes in the whole param (struct). If you do {{.name }} then the property of the struct is just used. You can use bigger data structures as well like maps. But you would use a template method of index.
With the template package you get some logic operations as well. Let’s look at the range operation.
Just use the keyword “range” and part of the struct you are going to range over.
{{range .ListContent}}
{{.}} Display data
Then the {{end}}
As in the example https://play.golang.org/p/JsVBCJG_k3K
Let’s break down how this can be used. First we need data to show. We use nested types to simplify code and a slice for simplicity as well.
You want to write out the structs to create a map of the data. You wouldn’t name these as such cause they are too generic.
Now we have the structs we can load them with data.
Then we utilize the template syntax to range over the data and do an if statement.
The {{.}} Is really from the range and the if statement just adds logic.
What is usually needed next is importing js and css files.
You are going to need this regardless of templates if you are building your own server and need to serve assets.
files := http.FileServer(http.Dir("assets/")) http.Handle("/static/", http.StripPrefix("/static/", files))
It seems simple but when you have a big directory structure this can get annoying. You have to do a lot of testing and restarting to make sure this connects.
The “assets/” directory is where the files are located on the server.
The /static/ is the URL used to access them on the site so you don’t display their Dir path.
Overview
- Template struct
- Where to use the template methods.
- Template syntax for logic
- Serving assets
After you have your template and assets you will naturally want to store data.
In the next article we will look at forms and a database.