mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-14 08:18:30 +00:00
Add first version of adddate to functions
This commit is contained in:
1 parent
144cc8ee04
commit
e7a9b447c9
3 files changed
+38
-5
No files matched your search
@@ -14,7 +14,42 @@ hugoversion:
|
|||||||
relatedfuncs: [now]
|
relatedfuncs: [now]
|
||||||
deprecated: false
|
deprecated: false
|
||||||
aliases: []
|
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>
|
||||||
|
— {{ .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 -->
|
||||||
@@ -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.
|
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" %}}
|
{{% code file="delimit-example-front-matter.toml" nocopy="true" %}}
|
||||||
```toml
|
```toml
|
||||||
@@ -41,8 +41,6 @@ tags: [ "tag1", "tag2", "tag3" ]
|
|||||||
```
|
```
|
||||||
{{% /code %}}
|
{{% /code %}}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{{% code file="delimit-page-tags-input.html" %}}
|
{{% code file="delimit-page-tags-input.html" %}}
|
||||||
```html
|
```html
|
||||||
<p>Tags: {{ delimit .Params.tags ", " }}</p>
|
<p>Tags: {{ delimit .Params.tags ", " }}</p>
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ Code that you put outside the block definitions *can* break your layout. This ev
|
|||||||
...your code here
|
...your code here
|
||||||
{{ end }}
|
{{ 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 %}}
|
{{% /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]:
|
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]:
|
||||||
|
|||||||
Reference in new issue
Block a user