You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-v1/get-started/template-usage.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,4 +99,4 @@ Let's take a look at the example how I would define a `home.tw.html` and then I'
99
99
- Then we insert the title into layout with the value "Home page"
100
100
- Then we insert the content into layout with the HTML body.
101
101
102
-
You can read more about [use](/docs/v1/language-elements/statements#use-statement), [insert](/docs/v1/language-elements/statements#insert-statement) and [reserve](/docs/v1/language-elements/statements#reserve-statement) statements on the [statements](/docs/v1/language-elements/statements) page if you need more information about the syntax.
102
+
You can read more about [use](/docs/v1/language-elements/statements#use-statement), [insert](/docs/v1/language-elements/statements#insert-statement) and [reserve](/docs/v1/language-elements/statements#reserve-statement) statements on the [statements](/docs/v1/language-elements/statements) page if you need more information about the syntax.
Copy file name to clipboardExpand all lines: versioned_docs/version-v2/guides/template-usage.md
+43-42Lines changed: 43 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ func main() {
29
29
fmt.Println(err)
30
30
}
31
31
32
-
http.HandleFunc("/", homeView)
32
+
http.HandleFunc("/", homeHandler)
33
33
34
34
http.ListenAndServe(":8080", nil)
35
35
}
@@ -42,50 +42,11 @@ Non of the configurations are required, because each configuration has a default
42
42
43
43
In return from the `NewTemplate` function, we get a `Template` object that can be used to evaluate an already parsed template.
44
44
45
-
## Configuration
46
-
There are a few configurations that you can pass to the `NewTemplate` function to configure the template language. The `NewTemplate` function accepts a `*config.Config` with several properties.
47
-
48
-
There are cases when you want to override the default file format or the directory where the template files are stored. Here is an example of how you can configure the template language:
49
-
50
-
```go title="main.go"
51
-
import (
52
-
"fmt"
53
-
"net/http"
54
-
55
-
"github.com/textwire/textwire/v2"
56
-
// highlight-next-line
57
-
"github.com/textwire/textwire/v2/config"
58
-
)
59
-
60
-
vartpl *textwire.Template
61
-
62
-
funcmain() {
63
-
varerrerror
64
-
65
-
// highlight-start
66
-
tpl, err = textwire.NewTemplate(&config.Config{
67
-
TemplateDir: "src/templates",
68
-
TemplateExt: ".tw", // recommended to use .tw extension
69
-
})
70
-
// highlight-end
71
-
72
-
if err != nil {
73
-
fmt.Println(err)
74
-
}
75
-
76
-
http.HandleFunc("/", homeView)
77
-
78
-
http.ListenAndServe(":8080", nil)
79
-
}
80
-
```
81
-
82
-
To read more about the available configurations, visit the [configurations](/docs/v2/guides/configurations) page.
83
-
84
45
## Write response to the client
85
46
You can use the `Response` method on `Template` object to write the evaluated template to the client. The `Response` method accepts a `http.ResponseWriter` object, the name of the template file, and a map of variables that you want to inject into the template. Here is an example:
86
47
87
48
```go title="main.go"
88
-
funchomeView(whttp.ResponseWriter, r *http.Request) {
49
+
funchomeHandler(whttp.ResponseWriter, r *http.Request) {
@@ -140,4 +101,44 @@ Let's take a look at the example how I would define a `home.tw.html` and then I'
140
101
- Then we insert the title into layout with the value "Home page"
141
102
- Then we insert the content into layout with the HTML body.
142
103
143
-
You can read more about [use](/docs/v2/language-elements/statements#use-statement), [insert](/docs/v2/language-elements/statements#insert-statement) and [reserve](/docs/v2/language-elements/statements#reserve-statement) statements on the [statements](/docs/v2/language-elements/statements) page if you need more information about the syntax.
104
+
You can read more about [use](/docs/v2/language-elements/statements#use-statement), [insert](/docs/v2/language-elements/statements#insert-statement) and [reserve](/docs/v2/language-elements/statements#reserve-statement) statements on the [statements](/docs/v2/language-elements/statements) page if you need more information about the syntax.
105
+
106
+
## Configuration
107
+
There are a few configurations that you can pass to the `NewTemplate` function to configure the template language. The `NewTemplate` function accepts a `*config.Config` with several properties.
108
+
109
+
There are cases when you want to override the default file format or the directory where the template files are stored. Here is an example of how you can configure the template language:
110
+
111
+
```go title="main.go"
112
+
import (
113
+
"fmt"
114
+
"net/http"
115
+
116
+
"github.com/textwire/textwire/v2"
117
+
// highlight-next-line
118
+
"github.com/textwire/textwire/v2/config"
119
+
)
120
+
121
+
vartpl *textwire.Template
122
+
123
+
funcmain() {
124
+
varerrerror
125
+
126
+
// highlight-start
127
+
tpl, err = textwire.NewTemplate(&config.Config{
128
+
TemplateDir: "src/templates",
129
+
TemplateExt: ".tw", // recommended to use .tw extension
130
+
})
131
+
// highlight-end
132
+
133
+
if err != nil {
134
+
fmt.Println(err)
135
+
}
136
+
137
+
http.HandleFunc("/", homeHandler)
138
+
139
+
http.ListenAndServe(":8080", nil)
140
+
}
141
+
```
142
+
143
+
To read more about the available configurations, visit the [configurations](/docs/v2/guides/configurations) page.
0 commit comments