2014-02-18 18:35:27 -05:00
---
2014-05-29 18:42:05 -04:00
aliases:
- /layout/functions/
date: 2013-07-01
linktitle: Functions
2014-04-23 03:00:11 -04:00
menu:
main:
2014-05-29 18:42:05 -04:00
parent: layout
next: /templates/variables
prev: /templates/go-templates
title: Hugo Template Functions
weight: 20
2014-02-18 18:35:27 -05:00
---
2014-08-31 07:08:36 -04:00
Hugo uses the excellent Go html/template library for its template engine.
2014-02-18 18:35:27 -05:00
It is an extremely lightweight engine that provides a very small amount of
2014-08-31 07:08:36 -04:00
logic. In our experience it is just the right amount of logic to be able
2014-02-18 18:35:27 -05:00
to create a good static website.
Go templates are lightweight but extensible. Hugo has added the following
functions to the basic template logic.
2014-09-10 12:42:58 -04:00
(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 ).)
2014-02-18 18:35:27 -05:00
## General
### isset
Return true if the parameter is set.
Takes either a slice, array or channel and an index or a map and a key as input.
2014-09-03 00:12:26 -04:00
e.g. {{ if isset .Params "project_url" }} {{ index .Params "project_url" }}{{ end }}
2014-02-18 18:35:27 -05:00
### echoParam
If parameter is set, then echo it.
2014-09-03 00:12:26 -04:00
e.g. {{echoParam .Params "project_url" }}
2014-02-18 18:35:27 -05:00
2014-09-09 09:26:51 -04:00
### eq
Return true if the parameters are equal.
e.g.
{{ if eq .Section "blog" }}current{{ end}}"
2014-02-18 18:35:27 -05:00
### first
Slices an array to only the first X elements.
2014-08-30 00:57:38 -04:00
Works on [lists ](/templates/list/ ), [taxonomies ](/taxonomies/displaying/ ), [terms ](/templates/terms/ ), [groups ](/templates/list/ )
2014-09-03 00:12:26 -04:00
e.g.
2014-02-18 18:35:27 -05:00
{{ range first 10 .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
2014-08-30 00:57:38 -04:00
### where
Filters an array to only elements containing a matching value for a given field.
Works on [lists ](/templates/list/ ), [taxonomies ](/taxonomies/displaying/ ), [terms ](/templates/terms/ ), [groups ](/templates/list/ )
2014-09-03 00:12:26 -04:00
e.g.
2014-08-30 00:57:38 -04:00
{{ range where .Data.Pages "Section" "post" }}
{{ .Content}}
{{ end }}
*where and first can be stacked*
2014-09-03 00:12:26 -04:00
e.g.
2014-08-30 00:57:38 -04:00
{{ range first 5 (where .Data.Pages "Section" "post") }}
{{ .Content}}
{{ end }}
2014-10-08 12:05:22 -04:00
### in
Checks if an element is in an array (or slice) and returns a boolean. The elements supported are strings, integers and floats (only float64 will match as expected). In addition, it can also check if a substring exists in a string.
e.g.
{{ if in .Params.tags "Git" }}Follow me on GitHub!{{ end }}
or
{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}
### intersect
Given two arrays (or slices), this function will return the common elements in the arrays. The elements supported are strings, integers and floats (only float64).
A useful example of this functionality is a 'similar posts' block. Create a list of links to posts where any of the tags in the current post match any tags in other posts.
e.g.
< ul >
{{ $page_link := .Permalink }}
{{ $tags := .Params.tags }}
{{ range .Site.Recent }}
{{ $page := . }}
{{ $has_common_tags := intersect $tags .Params.tags | len | lt 0 }}
{{ if and $has_common_tags (ne $page_link $page.Permalink) }}
< li > < a href = "{{ $page.Permalink }}" > {{ $page.Title }}< / a > < / li >
{{ end }}
{{ end }}
< / ul >
2014-02-18 18:35:27 -05:00
## Math
### add
Adds two integers.
2014-09-03 00:12:26 -04:00
e.g. {{add 1 2}} → 3
2014-02-18 18:35:27 -05:00
### sub
Subtracts two integers.
2014-09-03 00:12:26 -04:00
e.g. {{sub 3 2}} → 1
2014-02-18 18:35:27 -05:00
### div
Divides two integers.
2014-09-03 00:12:26 -04:00
e.g. {{div 6 3}} → 2
2014-02-18 18:35:27 -05:00
### mul
Multiplies two integers.
2014-09-03 00:12:26 -04:00
e.g. {{mul 2 3}} → 6
2014-02-18 18:35:27 -05:00
### mod
Modulus of two integers.
2014-09-03 00:12:26 -04:00
e.g. {{mod 15 3}} → 0
2014-02-18 18:35:27 -05:00
### modBool
Boolean of modulus of two integers.
true if modulus is 0.
2014-09-03 00:12:26 -04:00
e.g. {{modBool 15 3}} → true
2014-02-18 18:35:27 -05:00
## Strings
### urlize
2014-09-03 00:12:26 -04:00
Takes a string and sanitizes it for usage in URLs, converts spaces to "-".
2014-02-18 18:35:27 -05:00
2014-09-03 00:12:26 -04:00
e.g. < a href="/tags/{{ . | urlize }}"> {{ . }}< /a>
2014-02-18 18:35:27 -05:00
### safeHtml
2014-09-03 00:12:26 -04:00
Declares the provided string as "safe" so Go templates will not filter it.
2014-02-18 18:35:27 -05:00
2014-09-03 00:12:26 -04:00
e.g. {{ .Params.CopyrightHTML | safeHtml }}
2014-02-18 18:35:27 -05:00
### lower
Convert all characters in string to lowercase.
2014-09-03 00:12:26 -04:00
e.g. {{lower "BatMan"}} → "batman"
2014-02-18 18:35:27 -05:00
### upper
Convert all characters in string to uppercase.
2014-09-03 00:12:26 -04:00
e.g. {{upper "BatMan"}} → "BATMAN"
2014-02-18 18:35:27 -05:00
### title
Convert all characters in string to titlecase.
2014-09-03 00:12:26 -04:00
e.g. {{title "BatMan"}} → "Batman"
2014-02-18 18:35:27 -05:00
### highlight
2014-09-03 00:12:26 -04:00
Take a string of code and a language, uses Pygments to return the syntax
highlighted code in HTML. Used in the [highlight shortcode ](/extras/highlighting ).