Update 07.4.md
typographical errors and improved readability
This commit is contained in:
30
en/07.4.md
30
en/07.4.md
@@ -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 show 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, like 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:
|
||||
|
||||
@@ -24,7 +24,7 @@ Example:
|
||||
t.Execute(w, user) // merge.
|
||||
}
|
||||
|
||||
As you can see, it's very easy to use, load and render data in templates in Go, just like in other programming languages.
|
||||
As you can see, it's very easy to use, load and render data in templates in Go, just as in other programming languages.
|
||||
|
||||
For the sake of convenience, we will use the following rules in our examples:
|
||||
|
||||
@@ -34,7 +34,7 @@ For the sake of convenience, we will use the following rules in our examples:
|
||||
|
||||
## Inserting data into a template
|
||||
|
||||
We've just showed you how to parse and render templates. Let's take it one step further and render data to our templates. Every template is an object in Go, so how do we insert fields to templates?
|
||||
We've just shown you how to parse and render templates. Let's take it one step further and render data to our templates. Every template is an object in Go, so how do we insert fields to templates?
|
||||
|
||||
### Fields
|
||||
|
||||
@@ -69,11 +69,11 @@ The above example outputs `hello Astaxie` correctly, but if we modify our struct
|
||||
|
||||
This part of the code will not be compiled because we try to access a field that has not been exported. However, if we try to use a field that does not exist, Go simply outputs an empty string instead of an error.
|
||||
|
||||
If you print `{{.}}` in a template, Go outputs formatted string of this object, calling `fmt` under the covers.
|
||||
If you print `{{.}}` in a template, Go outputs a formatted string of this object, calling `fmt` under the covers.
|
||||
|
||||
### Nested fields
|
||||
|
||||
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.
|
||||
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*** ).
|
||||
@@ -152,7 +152,7 @@ Unix users should be familiar with the `pipe` operator, like `ls | grep "beego"`
|
||||
|
||||
{{. | html}}
|
||||
|
||||
We can use this method to escape the e-mail body to HTML. It's quite the same as writing a Unix command, and its convenient for use in template functions.
|
||||
We can use this method to escape the e-mail body to HTML. It's quite similar to writing a Unix command, and it is convenient for use in template functions.
|
||||
|
||||
### Template variables
|
||||
|
||||
@@ -168,7 +168,7 @@ More examples:
|
||||
|
||||
### Template functions
|
||||
|
||||
Go uses the `fmt` package to format output in templates, but sometimes we need to do something else. As an example scenario, let's say we want to replace `@` with `at` in our e-mail address, like `astaxie at beego.me`. At this point, we have to write a customized function.
|
||||
Go uses the `fmt` package to format output in templates, but sometimes we need to do something else. For example consider the following scenario: let's say we want to replace `@` with `at` in our e-mail address, like `astaxie at beego.me`. At this point, we have to write a customized function.
|
||||
|
||||
Every template function has a unique name and is associated with one function in your Go program as follows:
|
||||
|
||||
@@ -332,7 +332,7 @@ Main template:
|
||||
{{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"}}
|
||||
//otherwise the compiler wouldn't understand what to substitute when it reads the {{template "header"}}
|
||||
|
||||
Code:
|
||||
|
||||
@@ -374,7 +374,7 @@ Code:
|
||||
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.
|
||||
Here we can see that `template.ParseFiles` parses all nested templates into cache, and that every template defined by `{{define}}` are independent of each other. 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.
|
||||
|
||||
@@ -389,7 +389,7 @@ Some times you want to contextualize templates, for instance you have a `_head.h
|
||||
{{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
|
||||
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:
|
||||
|
||||
@@ -424,11 +424,11 @@ We use the task array and the Navigation in our templates, we saw how we use the
|
||||
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
|
||||
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 capital unless you
|
||||
want to make them private.
|
||||
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 }}
|
||||
@@ -457,7 +457,7 @@ This block of code will print each title and content of the Task array. Below is
|
||||
|
||||
## 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.
|
||||
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 (View) part of the MVC architecture. In the following chapters, we will cover the M (Model) and C (Controller) aspects of MVC.
|
||||
|
||||
## Links
|
||||
|
||||
|
||||
Reference in New Issue
Block a user