This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
go-template-recipes.html
358 lines (256 loc) · 14.3 KB
/
go-template-recipes.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html>
<head>
<title>Caltech Library's Digital Library Development Sandbox</title>
<link rel="stylesheet" href="/css/site.css">
</head>
<body>
<header>
<a href="http://library.caltech.edu"><img src="/assets/liblogo.gif" alt="Caltech Library logo"></a>
</header>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="../">Up</a></li>
<li><a href="the-basics.html">The Basics</a></li>
<li><a href="one-element/">One Element</a></li>
<li><a href="simple/">Simple</a></li>
<li><a href="simple-with-nav/">Simple w/Nav</a></li>
<li><a href="go-template-recipes.html">Template Recipes</a></li>
<li><a href="fountain-demo/">Foujntain Demo</a></li>
</ul>
</nav>
<section>
<h1>Go text/template recipes</h1>
<p><em>mkpage</em> template engine is Go’s <a href="https://golang.org/pkg/text/template/">text/template</a>. Go’s templates provide a flexible and simple <a href="https://en.wikipedia.org/wiki/Domain-specific_language">DSL</a> describing how to assemble a document based on a data structure passed to it. <em>mkpage</em> uses a list of key/value pairs on the command line to populate the data structure the template package expects. This includes support for JSON formatted text from strings, files and URL response. It also support transforming markdown content into HTML before assembling the final template.</p>
<p>While Go’s template package is not complicated to use it doesn’t come with allot of examples or tutorials. Most articles you find on Go’s template packages either focus on web server code or are for sophisticated static content generators like <a href="http://gohugo.io">Hugo</a>. Hugo extends Go’s template DSL providing capabilities that rival and surpass older static content generators like <a href="https://jekyllrb.com/">Jekyll</a> and <a href="http://jade-lang.com/">Jade</a>.</p>
<p><em>mkpage</em> uses Go v1.8’s text/template as is providing little in the way of extensions. <em>mkpage</em> is meant to be a trivially easy system for producing simple content from plain text, markdown text, and JSON. It deliberately implements a minimal feature set targetting a scripting environment like a Bash shell.</p>
<h2>only three data formats are supported</h2>
<p><em>mkpage</em> supports three document formats</p>
<ul>
<li>text/plain</li>
<li>text/markdown</li>
<li>application/json</li>
</ul>
<h2>only three data sources are supported</h2>
<p><em>mkpage</em> supports three data sources</p>
<ul>
<li>files (the default data source)</li>
<li>explicit strings (using a hint prefix, e.g. “text:”, “markdown:”, “json:”)</li>
<li>URLs as data sources (prefixed with http:// and https:// as appropriate)</li>
</ul>
<h2>Examples</h2>
<h3>Rendering a Markdown as HTML</h3>
<p>This is a minimal example of using <em>mkpage</em> to render a Markdown file as an HTML page.
It features no navigation, just a wrapping HTML document head (with link to CSS file) and body.</p>
<pre><code> {{ define "page.tmpl" }}
<!DOCTYPE html>
<html>
<head><link rel="stylesheet" href="/css/site.css"></head>
<body>
{{ .Content }}
</body>
</html>
{{ end }}
</code></pre>
<p>Rendering a markdown document named <em>myfile.md</em> as <em>myfile.html</em> would look like</p>
<pre><code class="language-shell"> mkpage Content=myfile.md page.tmpl > myfile.html
</code></pre>
<h3>explicit stings, a get well card</h3>
<p>In this example we want to add a name to a simple get well message.</p>
<p>Our template is called <strong>get-well.tmpl</strong>. It looks like</p>
<pre><code class="language-go"> {{ define "get-well.tmpl" }}
Dear {{ .name -}},
Hope you are feeling better today.
Sencerly,
Mojo Sam
{{ end }}
</code></pre>
<p>On the command line we can run <em>mkpage</em> with the following options</p>
<pre><code class="language-shell"> mkpage "name=text:Little Frieda" get-well.tmpl
</code></pre>
<p>The output would look like</p>
<pre><code class="language-text"> Little Frieda,
Hope you are feeling better today.
Sencerly,
Mojo Sam
</code></pre>
<h4>Explanation</h4>
<p>The key “name” has a string value of “Little Frieda”. The template indicates this needs to be included after the word “Dear”. The key “name” is proceeded by a period or dot. The substitution happens between the opening “{{” and closing “}}”. Notice the “-” before the closing “}}”. This tells the template engine to not allow spacas after the value and the next non-space character (i.e. the comma of the opening line).</p>
<h3>JSON data, a key/value blob report</h3>
<p>In this example we construct a JSON object as part of the key/value pairs on the command line and pass it through the blob.tmpl template that displays they pairs.</p>
<p>The command envokation looks like</p>
<pre><code class="language-shell"> mkpage 'blob=json:{"one":1,"two":2}' blob.tmpl
</code></pre>
<p>The template is a simple range construct</p>
<pre><code class="language-go"> {{ define "blob.tmpl" }}
{{range $key,$val := .blob }}
Key: {{ $key }} Value: {{ $val -}}
{{end}}
{{ end }}
</code></pre>
<p>Results in text like</p>
<pre><code class="language-text">
Key: one Value: 1
Key: two Value: 2
</code></pre>
<h4>Explanation</h4>
<p>We use the range function to iterate over the key/value pairs of our JSON object. Additionally we assign those values to the template variables called “<span class="math inline">\(key" and "\)</span>val”. These are then used to format our output. Also notice the trailing values “-” which supresses and extra new line.</p>
<h2>Files are data source</h2>
<h3>Wraping a Markdown document in HTML</h3>
<p>In this example we want to embed a “story” in a simple HTML document. The <em>story</em> is written in Markdown format. Here’s the simple template</p>
<pre><code class="language-go"> {{ define "simple-page.tmpl" }}
<!DOCTYPE html>
<html>
<head><title>Stories</title></head>
<body>
{{ .story }}
</body>
</html>
{{ end }}
</code></pre>
<p>The command line would look something like</p>
<pre><code class="language-shell"> mkpage "story=my-story.md" simple-page.tmpl > my-story.html
</code></pre>
<h4>Explanation</h4>
<p>On the command line <em>story</em> is assumed to point to a file named “my-story.md”. The reason a file is assumed is because there is no hint prefix or URL prefix at the start of the value. Because the file ends in the file extension “.md” it is assume to be a Markdown file and processed accordingly before being assemble in the template.</p>
<h2>URL as data source</h2>
<h3>JSON data, a weather forecast</h3>
<p>In this example we get the current weather forecast for Guam. The source of the weather information is <a href="http://noaa.gov">NOAA</a>’s <a href="http://weather.gov">National Weather Services</a> website. By including the parameter “FcstType=json” at the end of the URL you get a JSON version of the weather forecast rather than the HTML or XML alternatives.</p>
<ul>
<li>data source: <a href="http://forecast.weather.gov/MapClick.php?lat=13.47190933300044&lon=144.74977715100056&FcstType=json">http://forecast.weather.gov/MapClick.php?lat=13.47190933300044&lon=144.74977715100056&FcstType=json</a></li>
</ul>
<p>Our template will be call <strong>forecast.tmpl</strong>. It will be used to produce a Markdown file of weather related information obtained from the JSON response.</p>
<pre><code class="language-go"> {{ define "forecast.tmpl" }}
{{with $co := .forecast.currentobservation}}
Current Observation:
+ {{ $co.name }}
+ Elevation: {{ $co.elev }}
+ Latitude: {{ $co.latitude }}
+ Longitude: {{ $co.longitude }}
+ Date: {{ $co.Date }}
+ Temp: {{ $co.Temp }}
+ Dew Point: {{ $co.Dewp }}
+ Relative Humidity: {{ $co.Relh }}
+ Winds: {{ $co.Winds }}
+ Wind direction: {{ $co.Windd }}
+ Gust: {{ $co.Gust }}
+ Visibility: {{ $co.Visibility }}
{{end}}
Forecast:
{{range .forecast.data.text }}
+ {{ . -}}
{{end}}
{{ end }}
</code></pre>
<p>The command line for <em>mkpage</em> would look like</p>
<pre><code class="language-shell"> mkpage "forecast=http://forecast.weather.gov/MapClick.php?lat=13.47190933300044&lon=144.74977715100056&FcstType=json" forecast.tmpl
</code></pre>
<p>The resulting page should look something like</p>
<pre><code class="language-text">
Current Observation:
+ Agana, Guam International Airport
+ Elevation: 299
+ Latitude: 13.48
+ Longitude: 144.8
+ Date: 5 Aug 08:54 am ChST
+ Temp: 82
+ Dew Point: 79
+ Relative Humidity: 89
+ Winds: 12
+ Wind direction: 220
+ Gust: 0
+ Visibility: 10.00
Forecast:
+ Scattered showers and thunderstorms. Mostly cloudy, with a high near 84. Breezy, with a southwest wind 23 to 25 mph, with gusts as high as 32 mph. Chance of precipitation is 40%.
+ Scattered showers and thunderstorms. Mostly cloudy, with a low around 79. Breezy, with a southwest wind 15 to 20 mph, with gusts as high as 25 mph. Chance of precipitation is 40%.
+ Mostly cloudy, with a high near 88. Heat index values as high as 99. Breezy, with a southwest wind 17 to 21 mph, with gusts as high as 26 mph.
+ Mostly cloudy, with a low around 79. Southwest wind 13 to 17 mph, with gusts as high as 22 mph.
+ Mostly cloudy, with a high near 88. Southwest wind 14 to 17 mph, with gusts as high as 22 mph.
+ Mostly cloudy, with a low around 79.
+ Mostly sunny, with a high near 89.
+ Partly cloudy, with a low around 80.
+ Scattered showers and thunderstorms. Mostly cloudy, with a high near 89. Chance of precipitation is 40%.
+ Scattered showers and thunderstorms. Mostly cloudy, with a low around 79. Chance of precipitation is 40%.
+ Scattered showers and thunderstorms. Mostly cloudy, with a high near 89. Chance of precipitation is 40%.
+ Scattered showers and thunderstorms. Mostly cloudy, with a low around 79. Chance of precipitation is 40%.
+ Partly sunny, with a high near 89.
</code></pre>
<h2>JSON with Dashes in property names</h2>
<p>Some JSON APIs come with objects containing property names with dashes in them. This is valid JSON but throws a monkey wrench into addressing them with Go template’s usual dot notation. Go’s templates provides a <em>index</em> function to address those types of property names.</p>
<pre><code class="language-JSON"> [
{
"journal-title":"Favorite adventures",
"article-title": "Zamborra and Beyond",
"author-list":[
{"family-name": "Frieda", "other-name": "Little"},
{"family-name": "Sam", "other-name": "Mojo"},
{"family-name": "Flanders", "other-name": "Jack"}
]
}
]
</code></pre>
<p>Iterating over this list in a template</p>
<pre><code class="language-html"> <ul>
{{with .data -}}
<li>
{{with (index "article-title" .)}}Article: {{ . }}{{end -}}
{{with (index "journal-title" .)}} Journal: {{ . }}{{end -}}
{{with (index "author-list" .)}}<br />
Authors: {{range $i, $author := (index "author-list" .)}}
{{if gt $i 0}}; {{end-}}
{{index "family-name" $author}}, {{index "other-name" $author}}
{{end}}
{{end}}
</li>
{{- end}}
</ul>
</code></pre>
<p>The <em>index</em> function can be used to build a nested path too</p>
<pre><code> {{index "top-level" "middle-level" "bottom-level" .someData}}
</code></pre>
<h2>Including sub-templates</h2>
<p>In complex pages it is nice to be able to include sub templates. In this example
We have a <em>signature.tmpl</em> and <em>postscript.tmpl</em> files as sub templates to <em>letter.tmpl</em>.</p>
<h4>letter.tmpl</h4>
<pre><code> {{ define "letter.tmpl" }}
Dear {{ .ToName }},
Hope all is well. I will be with you shortly though not necessarily
on the same plane of existance.
{{template "signature.tmpl" .}}
{{template "postscript.tmpl" .}}
{{ end }}
</code></pre>
<h4>signature.tmpl</h4>
<pre><code> {{ define "signature.tmpl" }}
Sincerly,
{{ .Name }}, somewhere next door to reality
{{ end }}
</code></pre>
<h4>postscript.tmpl</h4>
<pre><code> {{ define "postscript.tmpl" }}
(P.S. What is comming at you is coming from you, {{rfc3339 "now"}})
{{ end }}
</code></pre>
<h4>Putting it all together</h4>
<pre><code> mkpage "ToName=text:Mojo Sam" "Name=text:Jack Flanders" letter.tmpl signature.tmpl postscript.tmpl
</code></pre>
<p>This should output somthing like</p>
<pre><code> Dear Mojo Sam,
Hope all is well. I will be with you shortly though not necessarily
on the same plane of existance.
Sincerly,
Jack Flanders, somewhere next door to reality
(P.S. What is comming at you is coming from you, 2016-12-12T16:17:14-08:00)
</code></pre>
</section>
<footer>
<span><h1><A href="http://caltech.edu">Caltech</a></h1></span>
<span>© 2020 <a href="https://www.library.caltech.edu/copyright">Caltech library</a></span>
<address>1200 E California Blvd, Mail Code 1-32, Pasadena, CA 91125-3200</address>
<span>Phone: <a href="tel:+1-626-395-3405">(626)395-3405</a></span>
<span><a href="mailto:library@caltech.edu">Email Us</a></span>
<a class="cl-hide" href="sitemap.xml">Site Map</a>
</footer>
</body>
</html>