Fix english 7.4 chapter preventing book to be built

This commit is contained in:
Denis Koltsov
2016-11-19 14:08:01 +01:00
parent b19cd5d8d3
commit f5182cb3c4

View File

@@ -9,11 +9,11 @@ The following demonstrates the template mechanism:
Figure 7.1 Template mechanism
Most of the content that web applications respond to clients with is static, and the dynamic parts are usually very small. For example, if you need to display a list users who have visited a page, only the user name would be dynamic. The style of the list remains the same. As you can see, templates are useful for reusing static content.
Most of the content that web applications respond to clients with is static, and the dynamic parts are usually very small. For example, if you need to display a list users who have visited a page, only the user name would be dynamic. The style of the list remains the same. As you can see, templates are useful for reusing static content.
## Templating in Go
In Go, we have the `template` package to help handle templates. We can use functions like `Parse`, `ParseFile` and `Execute` to load templates from plain text or files, then evaluate the dynamic parts, as shown in figure 7.1.
In Go, we have the `template` package to help handle templates. We can use functions like `Parse`, `ParseFile` and `Execute` to load templates from plain text or files, then evaluate the dynamic parts, as shown in figure 7.1.
Example:
@@ -75,8 +75,8 @@ If you print `{{.}}` in a template, Go outputs a formatted string of this object
We know how to output a field now. What if the field is an object, and it also has its own fields? How do we print them all in one loop? We can use `{{with …}}…{{end}}` and `{{range …}}{{end}}` for exactly that purpose.
- `{{range}}` just like range in Go.
- `{{with}}` lets you write the same object name once and use `.` as shorthand for it ( ***Similar to `with` in VB*** ).
- {% raw %}`{{range}}`{% endraw %} just like range in Go.
- {% raw %}`{{with}}`{% endraw %} lets you write the same object name once and use `.` as shorthand for it ( ***Similar to `with` in VB*** ).
More examples:
@@ -305,6 +305,7 @@ Here's a complete example, supposing that we have the following three files: `he
Main template:
{% raw %}
//header.tmpl
{{define "header"}}
<html>
@@ -330,10 +331,11 @@ Main template:
</body>
</html>
{{end}}
//When using subtemplating make sure that you have parsed each sub template file,
//When using subtemplating make sure that you have parsed each sub template file,
//otherwise the compiler wouldn't understand what to substitute when it reads the {{template "header"}}
{% endraw %}
Code:
package main
@@ -344,9 +346,9 @@ Code:
"io/ioutil"
"text/template"
)
var templates *template.Template
func main() {
var allFiles []string
files, err := ioutil.ReadDir("./templates")
@@ -359,13 +361,13 @@ Code:
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()
s2, _ := templates.LookUp("content.tmpl")
s2, _ := templates.LookUp("content.tmpl")
s2.ExecuteTemplate(os.Stdout, "content", nil)
fmt.Println()
s3, _ := templates.LookUp("footer.tmpl")
@@ -380,19 +382,19 @@ When you don't want to use `{{define}}`, then you can just create a text file wi
Templates in one set know each other, but you must parse them for every single set.
Some times you want to contextualize templates, for instance you have a `_head.html`, you might have a header who's value you have to populate based on which data you are loading for instance for a todo list manager you can have three categories `pending`, `completed`, `deleted`. for this suppose you have an if statement like this
Some times you want to contextualize templates, for instance you have a `_head.html`, you might have a header who's value you have to populate based on which data you are loading for instance for a todo list manager you can have three categories `pending`, `completed`, `deleted`. for this suppose you have an if statement like this
<title>{{if eq .Navigation "pending"}} Tasks
<title>{{if eq .Navigation "pending"}} Tasks
{{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted
{{ else if eq .Navigation "edit"}} Edit
{{end}}
</title>
Note: Go templates follow the Polish notation while performing the comparison where you give the operator first and the comparison value and the value to be compared with. The else if part is pretty straight forward
Typically we use a `{{ range }}` operator to loop through the context variable which we pass to the template while execution like this:
//present in views package
context := db.GetTasks("pending") //true when you want non deleted notes
homeTemplate.Execute(w, context)
@@ -412,31 +414,31 @@ We get the context object from the database as a struct object, the definition i
Search string
Message string
}
//present in database package
var task []types.Task
var context types.Context
context = types.Context{Tasks: task, Navigation: status}
//This line is in the database package where the context is returned back to the view.
We use the task array and the Navigation in our templates, we saw how we use the Navigation in the template,
We use the task array and the Navigation in our templates, we saw how we use the Navigation in the template,
we'll see how we'll use the actual task array in our template.
Here in the `{{ if .Tasks }}` we first check if the Tasks field of our context object which we passed to the template
while executing is empty or not. If it is not empty then we will range through that array to populate the title and
content of Task. The below example is very important when it comes to looping through an array in a template, we
start with the Range operator, then we can give any member of that struct as `{{.Name}}`, my Task structure has a
Title and a Content, (please note the capital T and C, they are exported names and they need to be capitalised unless you
want to make them private).
so {{ range .Tasks }}
{{ .Title }}
{{ .Content }}
{{ end }}
{{ range .Tasks }}
{{ .Title }}
{{ .Content }}
{{ end }}
This block of code will print each title and content of the Task array. Below is a full example from github.com/thewhitetulip/Tasks home.html template.
<div class="timeline">
{{ if .Tasks}} {{range .Tasks}}
<div class="note">