updated for template caching and subtemplating
This commit is contained in:
36
en/07.4.md
36
en/07.4.md
@@ -301,7 +301,7 @@ The sub-template is called using the following syntax:
|
||||
|
||||
{{template "sub-template"}}
|
||||
|
||||
Here's a complete example, supposing that we have the following three files: `header.tmpl`, `content.tmpl` and `footer.tmpl`.
|
||||
Here's a complete example, supposing that we have the following three files: `header.tmpl`, `content.tmpl` and `footer.tmpl` in the folder `templates`, we will read the folder and store the file names in a string array, which we will then use to parse files.
|
||||
|
||||
Main template:
|
||||
|
||||
@@ -330,6 +330,9 @@ Main template:
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
|
||||
//When using subtemplating make sure that you have parsed each sub template file,
|
||||
//otherwise the compiler wouldn't understand what to substitue when it reads the {{template "header"}}
|
||||
|
||||
Code:
|
||||
|
||||
@@ -338,22 +341,43 @@ Code:
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
|
||||
var templates *template.Template
|
||||
|
||||
func main() {
|
||||
s1, _ := template.ParseFiles("header.tmpl", "content.tmpl", "footer.tmpl")
|
||||
var allFiles []string
|
||||
files, err := ioutil.ReadDir("./templates")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
filename := file.Name()
|
||||
if strings.HasSuffix(filename, ".tmpl") {
|
||||
allFiles = append(allFiles, "./templates/"+filename)
|
||||
}
|
||||
}
|
||||
|
||||
templates, err = template.ParseFiles(allFiles...) #parses all .tmpl files in the 'templates' folder
|
||||
|
||||
s1, _ := templates.LookUp("header.tmpl")
|
||||
s1.ExecuteTemplate(os.Stdout, "header", nil)
|
||||
fmt.Println()
|
||||
s1.ExecuteTemplate(os.Stdout, "content", nil)
|
||||
s2, _ := templates.LookUp("content.tmpl")
|
||||
s2.ExecuteTemplate(os.Stdout, "content", nil)
|
||||
fmt.Println()
|
||||
s1.ExecuteTemplate(os.Stdout, "footer", nil)
|
||||
s3, _ := templates.LookUp("footer.tmpl")
|
||||
s3.ExecuteTemplate(os.Stdout, "footer", nil)
|
||||
fmt.Println()
|
||||
s1.Execute(os.Stdout, nil)
|
||||
s3.Execute(os.Stdout, nil)
|
||||
}
|
||||
|
||||
Here we can see that `template.ParseFiles` parses all nested templates into cache, and that every template defined by `{{define}}` is independent of one another. They are persisted in something like a map, where the template names are keys and the values are the template bodies. We can then use `ExecuteTemplate` to execute the corresponding sub-templates, so that the header and footer are independent and content contains them both. Note that if we try to execute `s1.Execute`, nothing will be outputted because there is no default sub-template available.
|
||||
|
||||
When you don't want to use `{{define}}`, then you can just create a text file with the name of the sub template, for instance `_head.tmpl` is a sub template which you'll use across your project then create this file in the templates folder, and use the normal syntax. Lookup cache is basically created so that you don't read the file every time you serve a request, because if you do, then you are wasting a lot of resources for reading a file which won't change unless the codebase is being rewritten, it doesn't make sense to parse the template files during each HTTP GET request, so the technique is used where we parse the files once and then do a `LookUp()` on the cache to execute the template when we need it to display data.
|
||||
|
||||
Templates in one set know each other, but you must parse them for every single set.
|
||||
|
||||
## Summary
|
||||
|
||||
Reference in New Issue
Block a user