forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-templates.html
268 lines (212 loc) · 13.9 KB
/
text-templates.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 文本模板</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'string-formatting';
}
if (e.key == "ArrowRight") {
window.location.href = 'regular-expressions';
}
}
</script>
<body>
<div class="example" id="text-templates">
<h2><a href="./">Go by Example 中文版</a>: 文本模板</h2>
<table>
<tr>
<td class="docs">
<p>Go 使用 <code>text/template</code> 包为创建动态内容或向用户显示自定义输出提供了内置支持。
一个名为 <code>html/template</code> 的兄弟软件包提供了相同的 API,但具有额外的安全功能,被用于生成 HTML。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/a6JSWGUefWK"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"os"</span>
<span class="s">"text/template"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们可以创建一个新模板,并从字符串解析其正文。
模板是静态文本和包含在”动作“中用于动态插入内容的混合体。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t1</span> <span class="o">:=</span> <span class="nx">template</span><span class="p">.</span><span class="nf">New</span><span class="p">(</span><span class="s">"t1"</span><span class="p">)</span>
<span class="nx">t1</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">t1</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="s">"Value is {{.}}\n"</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>或者,我们可以使用 <code>template.Must</code> 函数,在 <code>Parse</code> 错误时返回 panic。
这对于在全局作用域中初始化的模板非常有用。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t1</span> <span class="p">=</span> <span class="nx">template</span><span class="p">.</span><span class="nf">Must</span><span class="p">(</span><span class="nx">t1</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="s">"Value: {{.}}\n"</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>通过“执行”模板,我们生成其文本,其中包含其“动作”的具体值。
<code>{{.}}</code> 动作被作为参数传递给 <code>Execute</code> 的值所代替。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t1</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="s">"some text"</span><span class="p">)</span>
<span class="nx">t1</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="nx">t1</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span>
<span class="s">"Go"</span><span class="p">,</span>
<span class="s">"Rust"</span><span class="p">,</span>
<span class="s">"C++"</span><span class="p">,</span>
<span class="s">"C#"</span><span class="p">,</span>
<span class="p">})</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们将在下面使用辅助函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">Create</span> <span class="o">:=</span> <span class="kd">func</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">t</span> <span class="kt">string</span><span class="p">)</span> <span class="o">*</span><span class="nx">template</span><span class="p">.</span><span class="nx">Template</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">template</span><span class="p">.</span><span class="nf">Must</span><span class="p">(</span><span class="nx">template</span><span class="p">.</span><span class="nf">New</span><span class="p">(</span><span class="nx">name</span><span class="p">).</span><span class="nf">Parse</span><span class="p">(</span><span class="nx">t</span><span class="p">))</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>如果数据是一个结构体,我们可以使用 <code>{{.FieldName}}</code> 动作来访问其字段。
这些字段应该是导出的,以便在模板执行时可访问。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t2</span> <span class="o">:=</span> <span class="nf">Create</span><span class="p">(</span><span class="s">"t2"</span><span class="p">,</span> <span class="s">"Name: {{.Name}}\n"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">t2</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">Name</span> <span class="kt">string</span>
<span class="p">}{</span><span class="s">"Jane Doe"</span><span class="p">})</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这同样适用于 map;在 map 中没有限制键名的大小写。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t2</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="kt">string</span><span class="p">{</span>
<span class="s">"Name"</span><span class="p">:</span> <span class="s">"Mickey Mouse"</span><span class="p">,</span>
<span class="p">})</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>if/else 提供了条件执行模板。如果一个值是类型的默认值,例如 0、空字符串、空指针等,
则该值被认为是 false。
这个示例演示了另一个模板特性:使用 <code>-</code> 在动作中去除空格。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t3</span> <span class="o">:=</span> <span class="nf">Create</span><span class="p">(</span><span class="s">"t3"</span><span class="p">,</span>
<span class="s">"{{if . -}} yes {{else -}} no {{end}}\n"</span><span class="p">)</span>
<span class="nx">t3</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="s">"not empty"</span><span class="p">)</span>
<span class="nx">t3</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span> <span class="s">""</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>range 块允许我们遍历切片,数组,映射或通道。在 range 块内,<code>{{.}}</code> 动作被设置为迭代的当前项。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">t4</span> <span class="o">:=</span> <span class="nf">Create</span><span class="p">(</span><span class="s">"t4"</span><span class="p">,</span>
<span class="s">"Range: {{range .}}{{.}} {{end}}\n"</span><span class="p">)</span>
<span class="nx">t4</span><span class="p">.</span><span class="nf">Execute</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stdout</span><span class="p">,</span>
<span class="p">[]</span><span class="kt">string</span><span class="p">{</span>
<span class="s">"Go"</span><span class="p">,</span>
<span class="s">"Rust"</span><span class="p">,</span>
<span class="s">"C++"</span><span class="p">,</span>
<span class="s">"C#"</span><span class="p">,</span>
<span class="p">})</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="gp">$</span> go run templates.go
<span class="go">Value: some text
</span><span class="go">Value: 5
</span><span class="go">Value: [Go Rust C++ C#]
</span><span class="go">Name: Jane Doe
</span><span class="go">Name: Mickey Mouse
</span><span class="go">yes
</span><span class="go">no
</span><span class="go">Range: Go Rust C++ C# </span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="regular-expressions.html">正则表达式</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"os\"\u000A \"text/template\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' t1 :\u003D template.New(\"t1\")\u000A t1, err :\u003D t1.Parse(\"Value is {{.}}\\n\")\u000A if err !\u003D nil {\u000A panic(err)\u000A }\u000A');codeLines.push(' t1 \u003D template.Must(t1.Parse(\"Value: {{.}}\\n\"))\u000A');codeLines.push(' t1.Execute(os.Stdout, \"some text\")\u000A t1.Execute(os.Stdout, 5)\u000A t1.Execute(os.Stdout, []string{\u000A \"Go\",\u000A \"Rust\",\u000A \"C++\",\u000A \"C#\",\u000A })\u000A');codeLines.push(' Create :\u003D func(name, t string) *template.Template {\u000A return template.Must(template.New(name).Parse(t))\u000A }\u000A');codeLines.push(' t2 :\u003D Create(\"t2\", \"Name: {{.Name}}\\n\")\u000A');codeLines.push(' t2.Execute(os.Stdout, struct {\u000A Name string\u000A }{\"Jane Doe\"})\u000A');codeLines.push(' t2.Execute(os.Stdout, map[string]string{\u000A \"Name\": \"Mickey Mouse\",\u000A })\u000A');codeLines.push(' t3 :\u003D Create(\"t3\",\u000A \"{{if . -}} yes {{else -}} no {{end}}\\n\")\u000A t3.Execute(os.Stdout, \"not empty\")\u000A t3.Execute(os.Stdout, \"\")\u000A');codeLines.push(' t4 :\u003D Create(\"t4\",\u000A \"Range: {{range .}}{{.}} {{end}}\\n\")\u000A t4.Execute(os.Stdout,\u000A []string{\u000A \"Go\",\u000A \"Rust\",\u000A \"C++\",\u000A \"C#\",\u000A })\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>