Add first version of adddate to functions

This commit is contained in:
Ryan Watters
2017-03-03 19:20:50 -06:00
parent 144cc8ee04
commit e7a9b447c9
3 changed files with 38 additions and 5 deletions
+36 -1
View File
@@ -14,7 +14,42 @@ hugoversion:
relatedfuncs: [now]
deprecated: false
aliases: []
needsexamples: true
---
<!-- Look to example in docs site here: https://github.com/spf13/hugo/blob/master/docs/layouts/index.html--e.g., can be paired with now and where -->
The `AddDate` function takes three arguments in logical order of `years`, `months`, and `days`.
## Example `AddDate`: Randomized Tweets from the Last 2 Years
Let's assume you have a filed at `data/tweets.toml` that contains a long list of Tweets you've been collecting to display on your site's homepage. This file is filled with hundreds of `[[tweet]]` blocks; e.g.---
```toml
[[tweet]]
name = "Steve Francia"
twitter_handle = "@spf13"
quote = "I am the creator of Hugo. #metadocreference"
link = "https://twitter.com/spf13"
date = "2017-01-07T00:00:00Z"
```
Let's assume you want to grab Tweets from the last two years and present them in a random order. In conjunction with the [`where`](functions/where/) and [`now`](/functions/now/) functions, we can limit our range to the last two years via `now.AddDate -2 0 0`, which represents a point in time 2 years, 0 days, and 0 hours before the time of your last site build.
{{% code file="partials/templates/random-tweets.html" download="tweets.html" %}}
```html
{{ range where $.Site.Data.tweets.tweet "date" "ge" (now.AddDate -2 0 0) | shuffle }}
<div class="item">
<blockquote>
<p>
{{ .quote | safeHTML }}
</p>
&mdash; {{ .name }} ({{ .twitter_handle }}) <a href="{{ .link }}">
{{ dateFormat "January 2, 2006" .date }}
</a>
</blockquote>
</div>
{{ end }}
```
{{% /code %}}
<!-- Look to example in docs site here: https://github.com/spf13/hugo/blob/master/docs/layouts/index.html--e.g., can be paired with now and where -->
+1 -3
View File
@@ -30,7 +30,7 @@ aliases: []
To maintain a consistent output order, maps will be sorted by keys and only a slice of the values will be returned.
Examples of `delimit` use the following front matter:
The examples of `delimit` that follow all use the same front matter:
{{% code file="delimit-example-front-matter.toml" nocopy="true" %}}
```toml
@@ -41,8 +41,6 @@ tags: [ "tag1", "tag2", "tag3" ]
```
{{% /code %}}
{{% code file="delimit-page-tags-input.html" %}}
```html
<p>Tags: {{ delimit .Params.tags ", " }}</p>
+1 -1
View File
@@ -99,7 +99,7 @@ Code that you put outside the block definitions *can* break your layout. This ev
...your code here
{{ end }}
```
[See this thread from the discussion forums](https://discuss.gohugo.io/t/baseof-html-block-templates-and-list-types-results-in-empty-pages/5612/6)
[See this thread from the Hugo discussion forums.](https://discuss.gohugo.io/t/baseof-html-block-templates-and-list-types-results-in-empty-pages/5612/6)
{{% /warning %}}
The following shows how you can override both the `"main"` and `"title"` block areas from the base template with code unique to your [default single page template][singletemplate]: