more examples and explanation of go templating
authorAlex Dunn <dunn.alex@gmail.com>
Wed, 10 Sep 2014 16:42:58 +0000 (09:42 -0700)
committerspf13 <steve.francia@gmail.com>
Thu, 11 Sep 2014 21:22:08 +0000 (17:22 -0400)
Emphasizing to people (like me) who aren't familiar with Go that just because something's not mentioned in the Hugo docs doesn't mean it's not possible

docs/content/templates/functions.md
docs/content/templates/go-templates.md

index b84a4ff3507683fa65e5c164f260e5d5487fa79f..9da9476adcc896852f00d80fc863d820212230d8 100644 (file)
@@ -20,7 +20,9 @@ to create a good static website.
 Go templates are lightweight but extensible. Hugo has added the following
 functions to the basic template logic.
 
-Go documentation for the built-in functions can be found [here](http://golang.org/pkg/text/template/)
+(Go itself supplies built-in functions, including comparison operators
+and other basic tools; these are listed in the
+[Go template documentation](http://golang.org/pkg/text/template/#hdr-Functions).)
 
 ## General
 
index 0ee6624fdeda08976716c3eee88c0765e7b53523..9cd52889508d2890ead19ad80637a0432c141858 100644 (file)
@@ -20,7 +20,7 @@ template systems from different languages or frameworks, you will find a lot of
 similarities in Go templates.
 
 This document is a brief primer on using Go templates. The [Go docs][gohtmltemplate]
-provide more details.
+go into more depth and cover features that aren't mentioned here.
 
 ## Introduction to Go Templates
 
@@ -87,10 +87,17 @@ are useful for building websites. Functions are called by using their name
 followed by the required parameters separated by spaces. Template
 functions cannot be added without recompiling Hugo.
 
-**Example:**
+**Example 1: Adding numbers**
 
     {{ add 1 2 }}
 
+**Example 2: Comparing numbers**
+
+    {{ lt 1 2 }}
+
+(There are more boolean operators, detailed in the
+[template documentation](http://golang.org/pkg/text/template/#hdr-Functions).)
+
 ## Includes
 
 When including another template, you will pass to it the data it will be
@@ -142,7 +149,6 @@ range.
 `if`, `else`, `with`, `or` & `and` provide the framework for handling conditional
 logic in Go Templates. Like `range`, each statement is closed with `end`.
 
-
 Go Templates treat the following values as false:
 
 * false
@@ -351,3 +357,23 @@ so, such as in this example:
 
 [go]: http://golang.org/
 [gohtmltemplate]: http://golang.org/pkg/html/template/
+
+# Template example: Show only upcoming events
+
+Go allows you to do more than what's shown here.  Using Hugo's
+[`where`](/templates/functions/#toc_4) function and Go built-ins, we can list
+only the items from `content/events/` whose date (set in the front matter) is in
+the future:
+
+    <h4>Upcoming Events</h4>
+    <ul class="upcoming-events">
+    {{ range where .Data.Pages.ByDate "Section" "events" }}
+      {{ if ge .Date.Unix .Now.Unix }}
+        <li><span class="event-type">{{ .Type | title }} —</span>
+          {{ .Title }}
+          on <span class="event-date">
+          {{ .Date.Format "2 January at 3:04pm" }}</span>
+          at {{ .Params.place }}
+        </li>
+      {{ end }}
+    {{ end }}