mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
more examples and explanation of go templating
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
This commit is contained in:
parent
becd4fe337
commit
275e1c0d23
2 changed files with 32 additions and 4 deletions
|
@ -20,7 +20,9 @@ to create a good static website.
|
||||||
Go templates are lightweight but extensible. Hugo has added the following
|
Go templates are lightweight but extensible. Hugo has added the following
|
||||||
functions to the basic template logic.
|
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
|
## General
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ template systems from different languages or frameworks, you will find a lot of
|
||||||
similarities in Go templates.
|
similarities in Go templates.
|
||||||
|
|
||||||
This document is a brief primer on using Go templates. The [Go docs][gohtmltemplate]
|
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
|
## 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
|
followed by the required parameters separated by spaces. Template
|
||||||
functions cannot be added without recompiling Hugo.
|
functions cannot be added without recompiling Hugo.
|
||||||
|
|
||||||
**Example:**
|
**Example 1: Adding numbers**
|
||||||
|
|
||||||
{{ add 1 2 }}
|
{{ 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
|
## Includes
|
||||||
|
|
||||||
When including another template, you will pass to it the data it will be
|
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
|
`if`, `else`, `with`, `or` & `and` provide the framework for handling conditional
|
||||||
logic in Go Templates. Like `range`, each statement is closed with `end`.
|
logic in Go Templates. Like `range`, each statement is closed with `end`.
|
||||||
|
|
||||||
|
|
||||||
Go Templates treat the following values as false:
|
Go Templates treat the following values as false:
|
||||||
|
|
||||||
* false
|
* false
|
||||||
|
@ -351,3 +357,23 @@ so, such as in this example:
|
||||||
|
|
||||||
[go]: http://golang.org/
|
[go]: http://golang.org/
|
||||||
[gohtmltemplate]: http://golang.org/pkg/html/template/
|
[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 }}
|
||||||
|
|
Loading…
Reference in a new issue