{{ end }}
129 |
130 | {{ end }}
131 |
132 |
133 |
134 | ### Usage
135 | I simply changed:
136 |
137 | {% image full http://farm5.staticflickr.com/4136/4829260124_57712e570a_o_d.jpg "One of my favorite touristy-type photos. I secretly waited for the good light while we were "having fun" and took this. Only regret: a stupid pole in the top-left corner of the frame I had to clumsily get rid of at post-processing." ->http://www.flickr.com/photos/alexnormand/4829260124/in/set-72157624547713078/ %}
138 |
139 | to this (this example uses a slightly extended version named `fig`, different than the built-in `figure`):
140 |
141 | {{%/* fig class="full" src="http://farm5.staticflickr.com/4136/4829260124_57712e570a_o_d.jpg" title="One of my favorite touristy-type photos. I secretly waited for the good light while we were having fun and took this. Only regret: a stupid pole in the top-left corner of the frame I had to clumsily get rid of at post-processing." link="http://www.flickr.com/photos/alexnormand/4829260124/in/set-72157624547713078/" */%}}
142 |
143 | As a bonus, the shortcode named parameters are, arguably, more readable.
144 |
145 | ## Finishing touches
146 | ### Fix content
147 | Depending on the amount of customization that was done with each post with Jekyll, this step will require more or less effort. There are no hard and fast rules here except that `hugo server --watch` is your friend. Test your changes and fix errors as needed.
148 |
149 | ### Clean up
150 | You'll want to remove the Jekyll configuration at this point. If you have anything else that isn't used, delete it.
151 |
152 | ## A practical example in a diff
153 | [Hey, it's Alex](http://heyitsalex.net/) was migrated in less than a _father-with-kids day_ from Jekyll to Hugo. You can see all the changes (and screw-ups) by looking at this [diff](https://github.com/alexandre-normand/alexandre-normand/compare/869d69435bd2665c3fbf5b5c78d4c22759d7613a...b7f6605b1265e83b4b81495423294208cc74d610).
154 |
--------------------------------------------------------------------------------
/exampleSite/content/posts/goisforlovers.md:
--------------------------------------------------------------------------------
1 | +++
2 | title = "(Hu)go Template Primer"
3 | description = ""
4 | tags = [
5 | "go",
6 | "golang",
7 | "templates",
8 | "themes",
9 | "development",
10 | "demo",
11 | ]
12 | date = "2014-04-02"
13 | categories = [
14 | "Development",
15 | "golang",
16 | ]
17 | +++
18 |
19 | Hugo uses the excellent [go][] [html/template][gohtmltemplate] library for
20 | its template engine. It is an extremely lightweight engine that provides a very
21 | small amount of logic. In our experience that it is just the right amount of
22 | logic to be able to create a good static website. If you have used other
23 | template systems from different languages or frameworks you will find a lot of
24 | similarities in go templates.
25 |
26 | This document is a brief primer on using go templates. The [go docs][gohtmltemplate]
27 | provide more details.
28 |
29 | # Introduction to Go Templates
30 |
31 | Go templates provide an extremely simple template language. It adheres to the
32 | belief that only the most basic of logic belongs in the template or view layer.
33 | One consequence of this simplicity is that go templates parse very quickly.
34 |
35 | A unique characteristic of go templates is they are content aware. Variables and
36 | content will be sanitized depending on the context of where they are used. More
37 | details can be found in the [go docs][gohtmltemplate].
38 |
39 | ## Basic Syntax
40 |
41 | Go lang templates are html files with the addition of variables and
42 | functions.
43 |
44 | **Go variables and functions are accessible within {{ }}**
45 |
46 | Accessing a predefined variable "foo":
47 |
48 | {{ foo }}
49 |
50 | **Parameters are separated using spaces**
51 |
52 | Calling the add function with input of 1, 2:
53 |
54 | {{ add 1 2 }}
55 |
56 | **Methods and fields are accessed via dot notation**
57 |
58 | Accessing the Page Parameter "bar"
59 |
60 | {{ .Params.bar }}
61 |
62 | **Parentheses can be used to group items together**
63 |
64 | {{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }}
65 |
66 |
67 | ## Variables
68 |
69 | Each go template has a struct (object) made available to it. In hugo each
70 | template is passed either a page or a node struct depending on which type of
71 | page you are rendering. More details are available on the
72 | [variables](/layout/variables) page.
73 |
74 | A variable is accessed by referencing the variable name.
75 |
76 | {{ .Title }}
77 |
78 | Variables can also be defined and referenced.
79 |
80 | {{ $address := "123 Main St."}}
81 | {{ $address }}
82 |
83 |
84 | ## Functions
85 |
86 | Go template ship with a few functions which provide basic functionality. The go
87 | template system also provides a mechanism for applications to extend the
88 | available functions with their own. [Hugo template
89 | functions](/layout/functions) provide some additional functionality we believe
90 | are useful for building websites. Functions are called by using their name
91 | followed by the required parameters separated by spaces. Template
92 | functions cannot be added without recompiling hugo.
93 |
94 | **Example:**
95 |
96 | {{ add 1 2 }}
97 |
98 | ## Includes
99 |
100 | When including another template you will pass to it the data it will be
101 | able to access. To pass along the current context please remember to
102 | include a trailing dot. The templates location will always be starting at
103 | the /layout/ directory within Hugo.
104 |
105 | **Example:**
106 |
107 | {{ template "chrome/header.html" . }}
108 |
109 |
110 | ## Logic
111 |
112 | Go templates provide the most basic iteration and conditional logic.
113 |
114 | ### Iteration
115 |
116 | Just like in go, the go templates make heavy use of range to iterate over
117 | a map, array or slice. The following are different examples of how to use
118 | range.
119 |
120 | **Example 1: Using Context**
121 |
122 | {{ range array }}
123 | {{ . }}
124 | {{ end }}
125 |
126 | **Example 2: Declaring value variable name**
127 |
128 | {{range $element := array}}
129 | {{ $element }}
130 | {{ end }}
131 |
132 | **Example 2: Declaring key and value variable name**
133 |
134 | {{range $index, $element := array}}
135 | {{ $index }}
136 | {{ $element }}
137 | {{ end }}
138 |
139 | ### Conditionals
140 |
141 | If, else, with, or, & and provide the framework for handling conditional
142 | logic in Go Templates. Like range, each statement is closed with `end`.
143 |
144 |
145 | Go Templates treat the following values as false:
146 |
147 | * false
148 | * 0
149 | * any array, slice, map, or string of length zero
150 |
151 | **Example 1: If**
152 |
153 | {{ if isset .Params "title" }}
{{ index .Params "title" }}
{{ end }}
154 |
155 | **Example 2: If -> Else**
156 |
157 | {{ if isset .Params "alt" }}
158 | {{ index .Params "alt" }}
159 | {{else}}
160 | {{ index .Params "caption" }}
161 | {{ end }}
162 |
163 | **Example 3: And & Or**
164 |
165 | {{ if and (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
166 |
167 | **Example 4: With**
168 |
169 | An alternative way of writing "if" and then referencing the same value
170 | is to use "with" instead. With rebinds the context `.` within its scope,
171 | and skips the block if the variable is absent.
172 |
173 | The first example above could be simplified as:
174 |
175 | {{ with .Params.title }}
{{ . }}
{{ end }}
176 |
177 | **Example 5: If -> Else If**
178 |
179 | {{ if isset .Params "alt" }}
180 | {{ index .Params "alt" }}
181 | {{ else if isset .Params "caption" }}
182 | {{ index .Params "caption" }}
183 | {{ end }}
184 |
185 | ## Pipes
186 |
187 | One of the most powerful components of go templates is the ability to
188 | stack actions one after another. This is done by using pipes. Borrowed
189 | from unix pipes, the concept is simple, each pipeline's output becomes the
190 | input of the following pipe.
191 |
192 | Because of the very simple syntax of go templates, the pipe is essential
193 | to being able to chain together function calls. One limitation of the
194 | pipes is that they only can work with a single value and that value
195 | becomes the last parameter of the next pipeline.
196 |
197 | A few simple examples should help convey how to use the pipe.
198 |
199 | **Example 1 :**
200 |
201 | {{ if eq 1 1 }} Same {{ end }}
202 |
203 | is the same as
204 |
205 | {{ eq 1 1 | if }} Same {{ end }}
206 |
207 | It does look odd to place the if at the end, but it does provide a good
208 | illustration of how to use the pipes.
209 |
210 | **Example 2 :**
211 |
212 | {{ index .Params "disqus_url" | html }}
213 |
214 | Access the page parameter called "disqus_url" and escape the HTML.
215 |
216 | **Example 3 :**
217 |
218 | {{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
219 | Stuff Here
220 | {{ end }}
221 |
222 | Could be rewritten as
223 |
224 | {{ isset .Params "caption" | or isset .Params "title" | or isset .Params "attr" | if }}
225 | Stuff Here
226 | {{ end }}
227 |
228 |
229 | ## Context (aka. the dot)
230 |
231 | The most easily overlooked concept to understand about go templates is that {{ . }}
232 | always refers to the current context. In the top level of your template this
233 | will be the data set made available to it. Inside of a iteration it will have
234 | the value of the current item. When inside of a loop the context has changed. .
235 | will no longer refer to the data available to the entire page. If you need to
236 | access this from within the loop you will likely want to set it to a variable
237 | instead of depending on the context.
238 |
239 | **Example:**
240 |
241 | {{ $title := .Site.Title }}
242 | {{ range .Params.tags }}
243 |
244 | {{ end }}
245 |
246 | Notice how once we have entered the loop the value of {{ . }} has changed. We
247 | have defined a variable outside of the loop so we have access to it from within
248 | the loop.
249 |
250 | # Hugo Parameters
251 |
252 | Hugo provides the option of passing values to the template language
253 | through the site configuration (for sitewide values), or through the meta
254 | data of each specific piece of content. You can define any values of any
255 | type (supported by your front matter/config format) and use them however
256 | you want to inside of your templates.
257 |
258 |
259 | ## Using Content (page) Parameters
260 |
261 | In each piece of content you can provide variables to be used by the
262 | templates. This happens in the [front matter](/content/front-matter).
263 |
264 | An example of this is used in this documentation site. Most of the pages
265 | benefit from having the table of contents provided. Sometimes the TOC just
266 | doesn't make a lot of sense. We've defined a variable in our front matter
267 | of some pages to turn off the TOC from being displayed.
268 |
269 | Here is the example front matter:
270 |
271 | ```
272 | ---
273 | title: "Permalinks"
274 | date: "2013-11-18"
275 | aliases:
276 | - "/doc/permalinks/"
277 | groups: ["extras"]
278 | groups_weight: 30
279 | notoc: true
280 | ---
281 | ```
282 |
283 | Here is the corresponding code inside of the template:
284 |
285 | {{ if not .Params.notoc }}
286 |