Writing Functions
Here are the general steps to create a function using your content and Hugo built-ins. Here are some general tips:
printforscratchfor formatting and intermediate variableswith,if, andrangeto conditionally render blocks.Sitefor global data,.Paramsfor front matter
Start with the data:
.Content: rendered HTML.RawContent: raw markdown source.Title,.Date,.Params: common page fields
Apply functions with pipeline syntax:
{{ $words := countwords .Content }} {{ $readingTime := div $words 250 }}Use Hugo built-in functions:
countwords: counts wordslen: gets length of lists/maps/stringsadd,div,mul,sub: mathmath.Round,math.Floor,math.Ceiltime,now,dateFormatwhere,index,delimit,replace
For example, here we show the number of days since the page was last updated:
{{ $days := div (sub now.Unix .Lastmod.Unix) 86400 }}
<p>Updated {{ math.Floor $days }} days ago</p>