Merge pull request #566 from thewhitetulip/patch-7

updated more information about template
This commit is contained in:
astaxie
2015-11-24 11:22:48 +08:00

View File

@@ -380,6 +380,81 @@ 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
<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 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)
We get the context object from the database as a struct object, the definition is as below
//Task is the struct used to identify tasks
type Task struct {
Id int
Title string
Content string
Created string
}
//Context is the struct passed to templates
type Context struct {
Tasks []Task
Navigation string
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'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 execution 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 capital unless you
want to make them private.
so {{ 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">
<p class="noteHeading">{{.Title}}</p>
<hr>
<p class="noteContent">{{.Content}}</p>
</ul>
</span>
</div>
{{end}} {{else}}
<div class="note">
<p class="noteHeading">No Tasks here</p>
<p class="notefooter">
Create new task<button class="floating-action-icon-add" > here </button> </p>
</div>
{{end}}
## Summary
In this section, you learned how to combine dynamic data with templates using techniques including printing data in loops, template functions and nested templates. By learning about templates, we can conclude discussing the V part of the MVC architecture. In the following chapters, we will cover the M and C aspects of MVC.