Skip to content

Commit 83bf29f

Browse files
authored
Merge pull request astaxie#739 from mistadikay/master
Fix failing build for English version
2 parents ce406ed + a91ae34 commit 83bf29f

File tree

9 files changed

+57
-96
lines changed

9 files changed

+57
-96
lines changed

.gitignore

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pkg/*
33
*.html
44
*.exe
5-
<<<<<<< HEAD
6-
=======
7-
8-
>>>>>>> eead24cf064976b648de5826eab51880c803b0fa
5+
_book
6+
*.epub
7+
*.pdf
8+
.DS_Store

.gitignore.orig

-8
This file was deleted.

README.md.orig

-46
This file was deleted.

en/07.4.md

+29-27
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ The following demonstrates the template mechanism:
99

1010
Figure 7.1 Template mechanism
1111

12-
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.
12+
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.
1313

1414
## Templating in Go
1515

16-
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.
16+
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.
1717

1818
Example:
1919

@@ -75,8 +75,8 @@ If you print `{{.}}` in a template, Go outputs a formatted string of this object
7575

7676
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.
7777

78-
- `{{range}}` just like range in Go.
79-
- `{{with}}` lets you write the same object name once and use `.` as shorthand for it ( ***Similar to `with` in VB*** ).
78+
- {% raw %}`{{range}}`{% endraw %} just like range in Go.
79+
- {% raw %}`{{with}}`{% endraw %} lets you write the same object name once and use `.` as shorthand for it ( ***Similar to `with` in VB*** ).
8080

8181
More examples:
8282

@@ -305,6 +305,7 @@ Here's a complete example, supposing that we have the following three files: `he
305305

306306
Main template:
307307

308+
{% raw %}
308309
//header.tmpl
309310
{{define "header"}}
310311
<html>
@@ -330,10 +331,11 @@ Main template:
330331
</body>
331332
</html>
332333
{{end}}
333-
334-
//When using subtemplating make sure that you have parsed each sub template file,
334+
335+
//When using subtemplating make sure that you have parsed each sub template file,
335336
//otherwise the compiler wouldn't understand what to substitute when it reads the {{template "header"}}
336337

338+
{% endraw %}
337339
Code:
338340

339341
package main
@@ -344,9 +346,9 @@ Code:
344346
"io/ioutil"
345347
"text/template"
346348
)
347-
349+
348350
var templates *template.Template
349-
351+
350352
func main() {
351353
var allFiles []string
352354
files, err := ioutil.ReadDir("./templates")
@@ -359,13 +361,13 @@ Code:
359361
allFiles = append(allFiles, "./templates/"+filename)
360362
}
361363
}
362-
364+
363365
templates, err = template.ParseFiles(allFiles...) #parses all .tmpl files in the 'templates' folder
364-
366+
365367
s1, _ := templates.LookUp("header.tmpl")
366368
s1.ExecuteTemplate(os.Stdout, "header", nil)
367369
fmt.Println()
368-
s2, _ := templates.LookUp("content.tmpl")
370+
s2, _ := templates.LookUp("content.tmpl")
369371
s2.ExecuteTemplate(os.Stdout, "content", nil)
370372
fmt.Println()
371373
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
380382

381383
Templates in one set know each other, but you must parse them for every single set.
382384

383-
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
385+
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
384386

385-
<title>{{if eq .Navigation "pending"}} Tasks
387+
<title>{{if eq .Navigation "pending"}} Tasks
386388
{{ else if eq .Navigation "completed"}}Completed
387389
{{ else if eq .Navigation "deleted"}}Deleted
388390
{{ else if eq .Navigation "edit"}} Edit
389391
{{end}}
390392
</title>
391-
393+
392394
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
393395

394396
Typically we use a `{{ range }}` operator to loop through the context variable which we pass to the template while execution like this:
395-
397+
396398
//present in views package
397399
context := db.GetTasks("pending") //true when you want non deleted notes
398400
homeTemplate.Execute(w, context)
@@ -412,31 +414,31 @@ We get the context object from the database as a struct object, the definition i
412414
Search string
413415
Message string
414416
}
415-
417+
416418
//present in database package
417419
var task []types.Task
418420
var context types.Context
419421
context = types.Context{Tasks: task, Navigation: status}
420-
422+
421423
//This line is in the database package where the context is returned back to the view.
422-
423-
We use the task array and the Navigation in our templates, we saw how we use the Navigation in the template,
424+
425+
We use the task array and the Navigation in our templates, we saw how we use the Navigation in the template,
424426
we'll see how we'll use the actual task array in our template.
425-
427+
426428
Here in the `{{ if .Tasks }}` we first check if the Tasks field of our context object which we passed to the template
427429
while executing is empty or not. If it is not empty then we will range through that array to populate the title and
428430
content of Task. The below example is very important when it comes to looping through an array in a template, we
429431
start with the Range operator, then we can give any member of that struct as `{{.Name}}`, my Task structure has a
430432
Title and a Content, (please note the capital T and C, they are exported names and they need to be capitalised unless you
431433
want to make them private).
432-
433-
so {{ range .Tasks }}
434-
{{ .Title }}
435-
{{ .Content }}
436-
{{ end }}
437-
434+
435+
{{ range .Tasks }}
436+
{{ .Title }}
437+
{{ .Content }}
438+
{{ end }}
439+
438440
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.
439-
441+
440442
<div class="timeline">
441443
{{ if .Tasks}} {{range .Tasks}}
442444
<div class="note">

en/13.5.md

-10
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,7 @@ func (this *EditController) Get() {
127127
id, _ := strconv.Atoi(this.Ctx.Params[":id"])
128128
this.Data["Post"] = models.GetBlog(id)
129129
this.Layout = "layout.tpl"
130-
<<<<<<< HEAD
131-
this.TplNames = "new.tpl"
132-
=======
133130
this.TplNames = "edit.tpl"
134-
>>>>>>> eead24cf064976b648de5826eab51880c803b0fa
135131
}
136132
137133
func (this *EditController) Post() {
@@ -154,17 +150,11 @@ type DeleteController struct {
154150
}
155151
156152
func (this *DeleteController) Get() {
157-
<<<<<<< HEAD
158-
id, _ := strconv.Atoi(this.Ctx.Params[":id"])
159-
this.Data["Post"] = models.DelBlog(id)
160-
this.Ctx.Redirect(302, "/")
161-
=======
162153
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
163154
blog := models.GetBlog(id)
164155
this.Data["Post"] = blog
165156
models.DelBlog(blog)
166157
this.Ctx.Redirect(302, "/")
167-
>>>>>>> eead24cf064976b648de5826eab51880c803b0fa
168158
}
169159
```
170160

ja/src

-1
This file was deleted.
6 KB
Binary file not shown.

ja/src/1.2/main.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 章节 1.2
2+
// $GOPATH/src/mathapp/main.go
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"mymath"
9+
)
10+
11+
func main() {
12+
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
13+
}

ja/src/1.2/sqrt.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// 章节 1.2
2+
// $GOPATH/src/mymath/sqrt.go
3+
package mymath
4+
5+
func Sqrt(x float64) float64 {
6+
z := 0.0
7+
for i := 0; i < 1000; i++ {
8+
z -= (z*z - x) / (2 * x)
9+
}
10+
return z
11+
}

0 commit comments

Comments
 (0)