mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-29 23:20:49 +03:00
doc: Merge commit '2c0d1ccdcd
'
This commit is contained in:
commit
f387cb1b38
146 changed files with 1037 additions and 1443 deletions
|
@ -51,16 +51,14 @@ This code snippet---in the form of a [partial template][partials]---allows you t
|
|||
3. Sort the terms associated with the taxonomy by popularity.
|
||||
4. Grab the top two most popular terms in the taxonomy (i.e., the two most popular tags assigned to content.
|
||||
|
||||
{{% code file="grab-top-two-tags.html" %}}
|
||||
```html
|
||||
{{< code file="grab-top-two-tags.html" >}}
|
||||
<ul class="most-popular-tags">
|
||||
{{ $t := $.Site.GetPage "taxonomyTerm" "tags" }}
|
||||
{{ range first 2 $t.Data.Terms.ByCount }}
|
||||
<li>{{.}}</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
|
||||
[partials]: /templates/partials/
|
||||
|
|
|
@ -20,9 +20,9 @@ aliases: []
|
|||
|
||||
Both `absLangURL` and [`relLangURL`](/functions/rellangurl/) are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl) relatives but will add the correct language prefix when the site is configured with more than one language.
|
||||
|
||||
So for a site `baseURL` set to `http://yoursite.com/hugo/` and the current language is `en`:
|
||||
So for a site `baseURL` set to `http://example.com/hugo/` and the current language is `en`:
|
||||
|
||||
```golang
|
||||
{{ "blog/" | absLangURL }} → "http://yoursite.com/hugo/en/blog/"
|
||||
```
|
||||
{{ "blog/" | absLangURL }} → "http://example.com/hugo/en/blog/"
|
||||
{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
|
||||
```
|
||||
|
|
|
@ -18,10 +18,10 @@ deprecated: false
|
|||
aliases: []
|
||||
---
|
||||
|
||||
Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `http://yoursite.com/hugo/`:
|
||||
Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `http://example.com/hugo/`:
|
||||
|
||||
```golang
|
||||
{{ "mystyle.css" | absURL }} → "http://yoursite.com/hugo/mystyle.css"
|
||||
```
|
||||
{{ "mystyle.css" | absURL }} → "http://example.com/hugo/mystyle.css"
|
||||
{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
|
||||
{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
|
||||
{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
|
||||
|
@ -29,8 +29,7 @@ Both `absURL` and `relURL` consider the configured value of `baseURL` in your si
|
|||
|
||||
The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data (SEO)][jsonld], where some of your images for a piece of content may or may not be hosted locally:
|
||||
|
||||
{{% code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
|
||||
<script type="application/ld+json">
|
||||
{
|
||||
"@context" : "http://schema.org",
|
||||
|
@ -38,8 +37,7 @@ The last two examples may look strange but can be very useful. For example, the
|
|||
"image" : {{ apply .Params.images "absURL" "." }}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `<script>` tags. See [the safeJS template function][safejs] for examples of how to tell Hugo not to escape strings inside of such tags.
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ The `AddDate` function takes three arguments in logical order of `years`, `month
|
|||
|
||||
Let's assume you have a file at `data/tweets.toml` that contains a list of Tweets to display on your site's homepage. The file is filled with `[[tweet]]` blocks; e.g.---
|
||||
|
||||
```toml
|
||||
```
|
||||
[[tweet]]
|
||||
name = "Steve Francia"
|
||||
twitter_handle = "@spf13"
|
||||
|
@ -36,8 +36,7 @@ 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, you 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
|
||||
{{< code file="partials/templates/random-tweets.html" download="tweets.html" >}}
|
||||
{{ range where $.Site.Data.tweets.tweet "date" "ge" (now.AddDate -2 0 0) | shuffle }}
|
||||
<div class="item">
|
||||
<blockquote>
|
||||
|
@ -50,5 +49,4 @@ Let's assume you want to grab Tweets from the last two years and present them in
|
|||
</blockquote>
|
||||
</div>
|
||||
{{ end }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
|
|
@ -20,7 +20,7 @@ aliases: []
|
|||
|
||||
The following shows `after` being used in conjunction with the [`slice` function][slice]:
|
||||
|
||||
```html
|
||||
```
|
||||
{{ $data := slice "one" "two" "three" "four" }}
|
||||
{{ range after 2 $data }}
|
||||
{{ . }}
|
||||
|
@ -30,13 +30,12 @@ The following shows `after` being used in conjunction with the [`slice` function
|
|||
|
||||
## Example of `after` with `first`: 2nd–4th Most Recent Articles
|
||||
|
||||
You can use `after` in combination with the [`first` function][] and Hugo's [powerful sorting methods][lists]. Let's assume you have a list page at `yoursite.com/articles`. You have 10 articles, but you want your templating for the [list/section page][] to show only two rows:
|
||||
You can use `after` in combination with the [`first` function][] and Hugo's [powerful sorting methods][lists]. Let's assume you have a list page at `example.com/articles`. You have 10 articles, but you want your templating for the [list/section page][] to show only two rows:
|
||||
|
||||
1. The top row is titled "Featured" and shows only the most recently published article (i.e. by `publishdate` in the content files' front matter).
|
||||
2. The second row is titled "Recent Articles" and shows only the 2nd- to 4th-most recently published articles.
|
||||
|
||||
{{% code file="layouts/section/articles.html" download="articles.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/section/articles.html" download="articles.html" >}}
|
||||
{{ define "main" }}
|
||||
<section class="row featured-article">
|
||||
<h2>Featured Article</h2>
|
||||
|
@ -59,8 +58,7 @@ You can use `after` in combination with the [`first` function][] and Hugo's [pow
|
|||
{{ end }}
|
||||
</div>
|
||||
{{ end }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
[`first` function]: /functions/first/
|
||||
[list/section page]: /templates/section-templates/
|
||||
|
|
|
@ -31,7 +31,7 @@ aliases: []
|
|||
|
||||
Here is an example of a content file with `name:` as a front matter field:
|
||||
|
||||
```toml
|
||||
```
|
||||
+++
|
||||
names: [ "Derek Perkins", "Joe Bergevin", "Tanner Linsley" ]
|
||||
+++
|
||||
|
@ -39,7 +39,7 @@ names: [ "Derek Perkins", "Joe Bergevin", "Tanner Linsley" ]
|
|||
|
||||
You can then use `apply` as follows:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ apply .Params.names "urlize" "." }}
|
||||
```
|
||||
|
||||
|
@ -51,7 +51,7 @@ Which will result as follows:
|
|||
|
||||
This is *roughly* equivalent to using the following with [range][]
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ range .Params.names }}{{ . | urlize }}{{ end }}
|
||||
```
|
||||
|
||||
|
@ -59,8 +59,7 @@ However, it isn’t possible to provide the output of a range to the [`delimit`
|
|||
|
||||
If you have `post-tag-list.html` and `post-tag-link.html` as [partials][], you *could* use the following snippets, respectively:
|
||||
|
||||
{{% code file="layouts/partial/post-tag-list.html" copy="false" %}}
|
||||
```html
|
||||
{{< code file="layouts/partial/post-tag-list.html" copy="false" >}}
|
||||
{{ with .Params.tags }}
|
||||
<div class="tags-list">
|
||||
Tags:
|
||||
|
@ -76,20 +75,17 @@ If you have `post-tag-list.html` and `post-tag-link.html` as [partials][], you *
|
|||
{{ end }}
|
||||
</div>
|
||||
{{ end }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% code file="layouts/partial/post-tag-link.html" copy="false" %}}
|
||||
```html
|
||||
{{< code file="layouts/partial/post-tag-link.html" copy="false" >}}
|
||||
<a class="post-tag post-tag-{{ . | urlize }}" href="/tags/{{ . | urlize }}">{{ . }}</a>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
This works, but the complexity of `post-tag-list.html` is fairly high. The Hugo template needs to perform special behavior for the case where there’s only one tag, and it has to treat the last tag as special. Additionally, the tag list will be rendered something like `Tags: tag1 , tag2 , tag3` because of the way that the HTML is generated and then interpreted by a browser.
|
||||
|
||||
This first version of `layouts/partials/post-tag-list.html` separates all of the operations for ease of reading. The combined and DRYer version is shown next:
|
||||
|
||||
```html
|
||||
```
|
||||
{{ with .Params.tags }}
|
||||
<div class="tags-list">
|
||||
Tags:
|
||||
|
@ -103,16 +99,14 @@ This first version of `layouts/partials/post-tag-list.html` separates all of the
|
|||
|
||||
Now in the completed version, you can sort the tags, convert the tags to links with `layouts/partials/post-tag-link.html`, [chomp][] off stray newlines, and join the tags together in a delimited list for presentation. Here is an even DRYer version of the preceding example:
|
||||
|
||||
{{% code file="layouts/partials/post-tag-list.html" download="post-tag-list.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/post-tag-list.html" download="post-tag-list.html" >}}
|
||||
{{ with .Params.tags }}
|
||||
<div class="tags-list">
|
||||
Tags:
|
||||
{{ delimit (apply (apply (sort .) "partial" "post-tag-link" ".") "chomp" ".") ", " }}
|
||||
</div>
|
||||
{{ end }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% note %}}
|
||||
`apply` does not work when receiving the sequence as an argument through a pipeline.
|
||||
|
|
|
@ -21,15 +21,13 @@ aliases: []
|
|||
|
||||
An example:
|
||||
|
||||
{{% code file="base64-input.html" %}}
|
||||
```html
|
||||
{{< code file="base64-input.html" >}}
|
||||
<p>Hello world = {{ "Hello world" | base64Encode }}</p>
|
||||
<p>SGVsbG8gd29ybGQ = {{ "SGVsbG8gd29ybGQ=" | base64Decode }}</p>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% output file="base-64-output.html" %}}
|
||||
```html
|
||||
```
|
||||
<p>Hello world = SGVsbG8gd29ybGQ=</p>
|
||||
<p>SGVsbG8gd29ybGQ = Hello world</p>
|
||||
```
|
||||
|
@ -47,7 +45,7 @@ You can also pass other data types as arguments to the template function which t
|
|||
Using base64 to decode and encode becomes really powerful if we have to handle
|
||||
responses from APIs.
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ $resp := getJSON "https://api.github.com/repos/gohugoio/hugo/readme" }}
|
||||
{{ $resp.content | base64Decode | markdownify }}
|
||||
```
|
||||
|
|
|
@ -19,6 +19,6 @@ deprecated: false
|
|||
|
||||
Useful in a pipeline to remove newlines added by other processing (e.g., [`markdownify`](/functions/markdownify/)).
|
||||
|
||||
```golang
|
||||
```
|
||||
{{chomp "<p>Blockhead</p>\n"}} → "<p>Blockhead</p>"
|
||||
```
|
||||
|
|
|
@ -20,7 +20,7 @@ aliases: [/functions/countrunes/,/functions/countwords/]
|
|||
|
||||
In contrast with `countwords` function, which counts every word in a string, the `countrunes` function determines the number of runes in the content and excludes any whitespace. This has specific utility if you are dealing with CJK-like languages.
|
||||
|
||||
```html
|
||||
```
|
||||
{{ "Hello, 世界" | countrunes }}
|
||||
<!-- outputs a content length of 8 runes. -->
|
||||
```
|
||||
|
|
|
@ -20,7 +20,7 @@ aliases: [/functions/countrunes/,/functions/countwords/]
|
|||
|
||||
The template function works similar to the [.WordCount page variable][pagevars].
|
||||
|
||||
```html
|
||||
```
|
||||
{{ "Hugo is a static site generator." | countwords }}
|
||||
<!-- outputs a content length of 6 words. -->
|
||||
```
|
||||
|
|
|
@ -31,8 +31,7 @@ needsexamples: false
|
|||
|
||||
`default` function examples reference the following content page:
|
||||
|
||||
{{% code file="content/posts/default-function-example.md" %}}
|
||||
```yaml
|
||||
{{< code file="content/posts/default-function-example.md" >}}
|
||||
---
|
||||
title: Sane Defaults
|
||||
seo_title:
|
||||
|
@ -41,12 +40,11 @@ font:
|
|||
oldparam: The default function helps make your templating DRYer.
|
||||
newparam:
|
||||
---
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
`default` can be written in more than one way:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ index .Params "font" | default "Roboto" }}
|
||||
{{ default "Roboto" (index .Params "font") }}
|
||||
```
|
||||
|
@ -55,12 +53,10 @@ Both of the above `default` function calls return `Roboto`.
|
|||
|
||||
A `default` value, however, does not need to be hard coded like the previous example. The `default` value can be a variable or pulled directly from the front matter using dot notation:
|
||||
|
||||
{{% code file="variable-as-default-value.html" nocopy="true" %}}
|
||||
```golang
|
||||
{{< code file="variable-as-default-value.html" nocopy="true" >}}
|
||||
{{$old := .Params.oldparam }}
|
||||
<p>{{ .Params.newparam | default $old }}</p>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
Which would return:
|
||||
|
||||
|
@ -70,16 +66,14 @@ Which would return:
|
|||
|
||||
And then using dot notation
|
||||
|
||||
{{% code file="dot-notation-default-value.html" %}}
|
||||
```golang
|
||||
{{< code file="dot-notation-default-value.html" >}}
|
||||
<title>{{ .Params.seo_title | default .Title }}</title>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
Which would return
|
||||
|
||||
{{% output file="dot-notation-default-return-value.html" %}}
|
||||
```html
|
||||
```
|
||||
<title>Sane Defaults</title>
|
||||
```
|
||||
{{% /output %}}
|
||||
|
@ -88,18 +82,14 @@ The following have equivalent return values but are far less terse. This demonst
|
|||
|
||||
Using `if`:
|
||||
|
||||
{{% code file="if-instead-of-default.html" nocopy="true" %}}
|
||||
```golang
|
||||
{{< code file="if-instead-of-default.html" nocopy="true" >}}
|
||||
<title>{{if .Params.seo_title}}{{.Params.seo_title}}{{else}}{{.Title}}{{end}}</title>
|
||||
=> Sane Defaults
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
Using `with`:
|
||||
|
||||
{{% code file="with-instead-of-default.html" nocopy="true" %}}
|
||||
```golang
|
||||
{{< code file="with-instead-of-default.html" nocopy="true" >}}
|
||||
<title>{{with .Params.seo_title}}{{.}}{{else}}{{.Title}}{{end}}</title>
|
||||
=> Sane Defaults
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
|
|
@ -33,37 +33,31 @@ To maintain a consistent output order, maps will be sorted by keys and only a sl
|
|||
|
||||
The examples of `delimit` that follow all use the same front matter:
|
||||
|
||||
{{% code file="delimit-example-front-matter.toml" nocopy="true" %}}
|
||||
```toml
|
||||
{{< code file="delimit-example-front-matter.toml" nocopy="true" >}}
|
||||
+++
|
||||
title: I love Delimit
|
||||
#tags: [ "tag1", "tag2", "tag3" ]
|
||||
+++
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% code file="delimit-page-tags-input.html" %}}
|
||||
```html
|
||||
{{< code file="delimit-page-tags-input.html" >}}
|
||||
<p>Tags: {{ delimit .Params.tags ", " }}</p>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% output file="delimit-page-tags-output.html" %}}
|
||||
```html
|
||||
```
|
||||
<p>Tags: tag1, tag2, tag3</p>
|
||||
```
|
||||
{{% /output %}}
|
||||
|
||||
Here is the same example but with the optional "last" delimiter:
|
||||
|
||||
{{% code file="delimit-page-tags-final-and-input.html" %}}
|
||||
```golang
|
||||
{{< code file="delimit-page-tags-final-and-input.html" >}}
|
||||
Tags: {{ delimit .Params.tags ", " ", and " }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% output file="delimit-page-tags-final-and-output.html" %}}
|
||||
```html
|
||||
```
|
||||
<p>Tags: tag1, tag2, and tag3</p>
|
||||
```
|
||||
{{% /output %}}
|
||||
|
|
|
@ -26,21 +26,17 @@ aliases: []
|
|||
|
||||
The partial below creates a SVG and expects `fill` `height` and `width` from the caller:
|
||||
|
||||
{{% code file="layouts/partials/svgs/external-links.svg" download="external-links.svg" %}}
|
||||
```xml
|
||||
{{< code file="layouts/partials/svgs/external-links.svg" download="external-links.svg" >}}
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="{{ .fill }}" width="{{ .size }}" height="{{ .size }}" viewBox="0 0 32 32" aria-label="External Link">
|
||||
<path d="M25.152 16.576v5.696q0 2.144-1.504 3.648t-3.648 1.504h-14.848q-2.144 0-3.648-1.504t-1.504-3.648v-14.848q0-2.112 1.504-3.616t3.648-1.536h12.576q0.224 0 0.384 0.16t0.16 0.416v1.152q0 0.256-0.16 0.416t-0.384 0.16h-12.576q-1.184 0-2.016 0.832t-0.864 2.016v14.848q0 1.184 0.864 2.016t2.016 0.864h14.848q1.184 0 2.016-0.864t0.832-2.016v-5.696q0-0.256 0.16-0.416t0.416-0.16h1.152q0.256 0 0.416 0.16t0.16 0.416zM32 1.152v9.12q0 0.48-0.352 0.8t-0.8 0.352-0.8-0.352l-3.136-3.136-11.648 11.648q-0.16 0.192-0.416 0.192t-0.384-0.192l-2.048-2.048q-0.192-0.16-0.192-0.384t0.192-0.416l11.648-11.648-3.136-3.136q-0.352-0.352-0.352-0.8t0.352-0.8 0.8-0.352h9.12q0.48 0 0.8 0.352t0.352 0.8z"></path>
|
||||
</svg>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
These values can be stored in one object with `dict` and passed to the partial:
|
||||
|
||||
{{% code file="layouts/_default/list.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/_default/list.html" >}}
|
||||
{{ partial "svg/link-ext.svg" (dict "fill" "#01589B" "size" 10 "width" 20 ) }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ The example below returns a list of all second level headers (`<h2>`) in the con
|
|||
|
||||
You can limit the number of matches in the list with a third parameter. The following example shows how to limit the returned value to just one match (or none, if there are no matched substrings):
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ findRE "<h2.*?>(.|\n)*?</h2>" .Content 1 }}
|
||||
<!-- returns ["<h2 id="#foo">Foo</h2>"] -->
|
||||
```
|
||||
|
|
|
@ -20,7 +20,7 @@ aliases: []
|
|||
---
|
||||
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ range first 10 .Data.Pages }}
|
||||
{{ .Render "summary" }}
|
||||
{{ end }}
|
||||
|
|
|
@ -27,7 +27,7 @@ toc: true
|
|||
|
||||
Assuming a key-value of `date: 2017-03-03` in a content file's front matter, your can run the date through `.Format` followed by a layout string for your desired output at build time:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ .PublishDate.Format "January 2, 2006" }} => March 3, 2017
|
||||
```
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ aliases: []
|
|||
|
||||
This translates a piece of content based on your `i18n/en-US.yaml` (and similar) files. You can use the [go-i18n](https://github.com/nicksnyder/go-i18n) tools to manage your translations. The translations can exist in both the theme and at the root of your repository.
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ i18n "translation_id" }}
|
||||
```
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ relatedfuncs: []
|
|||
deprecated: false
|
||||
---
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ with (imageConfig "favicon.ico") }}
|
||||
favicon.ico: {{.Width}} x {{.Height}}
|
||||
{{ end }}
|
||||
|
|
|
@ -44,7 +44,7 @@ Assume you want to add a `location = ""` field to your front matter for every ar
|
|||
|
||||
Here is an example of the data inside `data/locations/oslo.toml`:
|
||||
|
||||
```toml
|
||||
```
|
||||
website = "https://www.oslo.kommune.no"
|
||||
pop_city = 658390
|
||||
pop_metro = 1717900
|
||||
|
@ -52,7 +52,7 @@ pop_metro = 1717900
|
|||
|
||||
The example we will use will be an article on Oslo, which front matter should set to exactly the same name as the corresponding file name in `data/locations/`:
|
||||
|
||||
```toml
|
||||
```
|
||||
title = "My Norwegian Vacation"
|
||||
location = "oslo"
|
||||
```
|
||||
|
@ -70,14 +70,14 @@ This is where the `index` function is needed. `index` takes 2 parameters in this
|
|||
|
||||
The variable for `.Params.location` is a string and can therefore replace `oslo` in the example above:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ index .Site.Data.authors .Params.author }}
|
||||
=> map[website:https://www.oslo.kommune.no pop_city:658390 pop_metro:1717900]
|
||||
```
|
||||
|
||||
Now the call will return the specific file according to the location specified in the content's front matter, but you will likely want to write specific properties to the template. You can do this by continuing down the node path via dot notation (`.`):
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ (index .Site.Data.locations .Params.location).pop_city }}
|
||||
=> 658390
|
||||
```
|
||||
|
|
|
@ -25,8 +25,7 @@ A useful example of `intersect` functionality is a "related posts" block. `isset
|
|||
|
||||
The following is an example of a "related posts" [partial template][partials] that could be added to a [single page template][single]:
|
||||
|
||||
{{% code file="layouts/partials/related-posts.html" download="related-posts.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/related-posts.html" download="related-posts.html" >}}
|
||||
<ul>
|
||||
{{ $page_link := .Permalink }}
|
||||
{{ $tags := .Params.tags }}
|
||||
|
@ -38,12 +37,11 @@ The following is an example of a "related posts" [partial template][partials] th
|
|||
{{ end }}
|
||||
{{ end }}
|
||||
</ul>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
This is also very useful to use as `AND` filters when combined with where:
|
||||
|
||||
```html
|
||||
```
|
||||
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
|
||||
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
|
||||
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
|
||||
|
|
|
@ -35,24 +35,20 @@ aliases: []
|
|||
|
||||
You may want to append a class to a heading according to the length of the string therein. The following templating checks to see if the title's length is greater than 80 characters and, if so, adds a `long-title` class to the `<h1>`:
|
||||
|
||||
{{% code file="check-title-length.html" %}}
|
||||
```html
|
||||
{{< code file="check-title-length.html" >}}
|
||||
<header>
|
||||
<h1{{if gt (len .Title) 80}} class="long-title"{{end}}>{{.Title}}</h1>
|
||||
</header>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
## `len` Example 2: Counting Pages with `where`
|
||||
|
||||
The following templating uses [`where`][] in conjunction with `len` to figure out the total number of content pages in a `posts` [section][]:
|
||||
|
||||
{{% code file="how-many-posts.html" %}}
|
||||
```html
|
||||
{{< code file="how-many-posts.html" >}}
|
||||
{{ $posts := (where .Site.RegularPages "Section" "==" "post") }}
|
||||
{{ $postCount := len $posts }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
Note the use of `.RegularPages`, a [site variable][] that counts all regular content pages but not the `_index.md` pages used to add front matter and content to [list templates][].
|
||||
|
||||
|
|
|
@ -37,13 +37,13 @@ You can also use the `add` function with strings. You may like this functionalit
|
|||
|
||||
For example, social media sharing with [Twitter Cards][cards] requires the following `meta` link in your site's `<head>` to display Twitter's ["Summary Card with Large Image"][twtsummary]:
|
||||
|
||||
```html
|
||||
<meta name="twitter:image" content="http://yoursite.com/images/my-twitter-image.jpg">
|
||||
```
|
||||
<meta name="twitter:image" content="http://example.com/images/my-twitter-image.jpg">
|
||||
```
|
||||
|
||||
Let's assume you have an `image` field in the front matter of each of your content files:
|
||||
|
||||
```yaml
|
||||
```
|
||||
---
|
||||
title: My Post
|
||||
image: my-post-image.jpg
|
||||
|
@ -52,12 +52,10 @@ image: my-post-image.jpg
|
|||
|
||||
You can then concatenate the `image` value (string) with the path to your `images` directory in `static` and leverage a URL-related templating function for increased flexibility:
|
||||
|
||||
{{% code file="partials/head/twitter-card.html" %}}
|
||||
```html
|
||||
{{< code file="partials/head/twitter-card.html" >}}
|
||||
{{$socialimage := add "images/" .Params.image}}
|
||||
<meta name="twitter:image" content="{{ $socialimage | absURL }}">
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
{{% note %}}
|
||||
The `add` example above makes use of the [`absURL` function](/functions/absurl/). `absURL` and its relative companion `relURL` is the recommended way to construct URLs in Hugo.
|
||||
|
|
|
@ -21,13 +21,13 @@ aliases: []
|
|||
|
||||
|
||||
|
||||
```html
|
||||
```
|
||||
{{ md5 "Hello world, gophers!" }}
|
||||
<!-- returns the string "b3029f756f98f79e7f1b7f1d1f0dd53b" -->
|
||||
```
|
||||
|
||||
This can be useful if you want to use [Gravatar](https://en.gravatar.com/) for generating a unique avatar:
|
||||
|
||||
```html
|
||||
```
|
||||
<img src="https://www.gravatar.com/avatar/{{ md5 "your@email.com" }}?s=100&d=identicon">
|
||||
```
|
||||
|
|
|
@ -23,7 +23,7 @@ See [`time.Time`](https://godoc.org/time#Time).
|
|||
|
||||
For example, building your site on June 24, 2017 with the following templating:
|
||||
|
||||
```html
|
||||
```
|
||||
<div>
|
||||
<small>© {{ now.Format "2006"}}</small>
|
||||
</div>
|
||||
|
@ -31,7 +31,7 @@ For example, building your site on June 24, 2017 with the following templating:
|
|||
|
||||
Which will produce the following:
|
||||
|
||||
```html
|
||||
```
|
||||
<div>
|
||||
<small>© 2017</small>
|
||||
</div>
|
||||
|
|
|
@ -21,17 +21,15 @@ aliases: []
|
|||
|
||||
The `partialCached` template function can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation. Here is the simplest usage:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ partialCached "footer.html" . }}
|
||||
```
|
||||
|
||||
You can also pass additional parameters to `partialCached` to create *variants* of the cached partial. For example, if you have a complex partial that should be identical when rendered for pages within the same section, you could use a variant based upon section so that the partial is only rendered once per section:
|
||||
|
||||
{{% code file="partial-cached-example.html" %}}
|
||||
```
|
||||
{{< code file="partial-cached-example.html" >}}
|
||||
{{ partialCached "footer.html" . .Section }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
If you need to pass additional parameters to create unique variants, you can pass as many variant parameters as you need:
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ deprecated: false
|
|||
|
||||
See [the go doc](https://golang.org/pkg/fmt/) for additional information.
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ i18n ( printf "combined_%s" $var ) }}
|
||||
```
|
||||
|
||||
|
|
|
@ -23,12 +23,12 @@ aliases: []
|
|||
|
||||
The following example creates a link to a search results page on Google.
|
||||
|
||||
```html
|
||||
```
|
||||
<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>
|
||||
```
|
||||
|
||||
This example renders the following HTML:
|
||||
|
||||
```html
|
||||
```
|
||||
<a href="https://www.google.com?page=3&q=test">Search</a>
|
||||
```
|
||||
|
|
|
@ -22,7 +22,7 @@ Note that the filename must be relative to the current project working directory
|
|||
|
||||
So, if you have a file with the name `README.txt` in the root of your project with the content `Hugo Rocks!`:
|
||||
|
||||
```html
|
||||
```
|
||||
{{readFile "README.txt"}} → "Hugo Rocks!"
|
||||
```
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ aliases: []
|
|||
|
||||
`ref` and `relRef` look up a content page by relative path (`relref`) or logical name (`ref`) to return the permalink. Both functions require a `Page` object (usually satisfied with a "`.`"):
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ relref . "about.md" }}
|
||||
```
|
||||
|
||||
|
|
|
@ -20,10 +20,10 @@ aliases: []
|
|||
|
||||
`absLangURL` and `relLangURL` functions are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl/) relatives but will add the correct language prefix when the site is configured with more than one language. (See [Configuring Multilingual][multiliconfig].)
|
||||
|
||||
So for a site `baseURL` set to `http://yoursite.com/hugo/` and the current language is `en`:
|
||||
So for a site `baseURL` set to `http://example.com/hugo/` and the current language is `en`:
|
||||
|
||||
```golang
|
||||
{{ "blog/" | absLangURL }} → "http://yoursite.com/hugo/en/blog/"
|
||||
```
|
||||
{{ "blog/" | absLangURL }} → "http://example.com/hugo/en/blog/"
|
||||
{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
|
||||
```
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ aliases: []
|
|||
|
||||
`ref` and `relRef` look up a content page by relative path (`relref`) or logical name (`ref`) to return the permalink. Both functions require a `Page` object (usually satisfied with a "`.`"):
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ relref . "about.md" }}
|
||||
```
|
||||
|
||||
|
|
|
@ -18,10 +18,10 @@ deprecated: false
|
|||
aliases: []
|
||||
---
|
||||
|
||||
Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `http://yoursite.com/hugo/`:
|
||||
Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `http://example.com/hugo/`:
|
||||
|
||||
```golang
|
||||
{{ "mystyle.css" | absURL }} → "http://yoursite.com/hugo/mystyle.css"
|
||||
```
|
||||
{{ "mystyle.css" | absURL }} → "http://example.com/hugo/mystyle.css"
|
||||
{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
|
||||
{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
|
||||
{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
|
||||
|
@ -29,8 +29,7 @@ Both `absURL` and `relURL` consider the configured value of `baseURL` in your si
|
|||
|
||||
The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data for SEO][jsonld] where some of your images for a piece of content may or may not be hosted locally:
|
||||
|
||||
{{% code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
|
||||
<script type="application/ld+json">
|
||||
{
|
||||
"@context" : "http://schema.org",
|
||||
|
@ -38,8 +37,7 @@ The last two examples may look strange but can be very useful. For example, the
|
|||
"image" : {{ apply .Params.images "absURL" "." }}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `<script>` tags. See [the safeJS template function][safejs] for examples of how to tell Hugo not to escape strings inside of such tags.
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ This function is only available when applied to a single piece of content within
|
|||
|
||||
This example could render a piece of content using the content view located at `/layouts/_default/summary.html`:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ range .Data.Pages }}
|
||||
{{ .Render "summary"}}
|
||||
{{ end }}
|
||||
|
|
|
@ -19,7 +19,7 @@ deprecated: false
|
|||
aliases: []
|
||||
---
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ replaceRE "^https?://([^/]+).*" "$1" "http://gohugo.io/docs" }}` → "gohugo.io"
|
||||
{{ "http://gohugo.io/docs" | replaceRE "^https?://([^/]+).*" "$1" }}` → "gohugo.io"
|
||||
```
|
||||
|
|
|
@ -22,19 +22,19 @@ It should not be used for HTML from a third-party, or HTML with unclosed tags or
|
|||
|
||||
Given a site-wide [`config.toml`][config] with the following `copyright` value:
|
||||
|
||||
```toml
|
||||
```
|
||||
copyright = "© 2015 Jane Doe. <a href=\"http://creativecommons.org/licenses/by/4.0/\">Some rights reserved</a>."
|
||||
```
|
||||
|
||||
`{{ .Site.Copyright | safeHTML }}` in a template would then output:
|
||||
|
||||
```html
|
||||
```
|
||||
© 2015 Jane Doe. <a href="http://creativecommons.org/licenses/by/4.0/">Some rights reserved</a>.
|
||||
```
|
||||
|
||||
However, without the `safeHTML` function, html/template assumes `.Site.Copyright` to be unsafe and therefore escapes all HTML tags and renders the whole string as plain text:
|
||||
|
||||
```html
|
||||
```
|
||||
<p>© 2015 Jane Doe. <a href="http://creativecommons.org/licenses by/4.0/">Some rights reserved</a>.</p>
|
||||
```
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ aliases: []
|
|||
|
||||
Example: Given a site-wide `config.toml` that contains this menu entry:
|
||||
|
||||
```toml
|
||||
```
|
||||
[[menu.main]]
|
||||
name = "IRC: #golang at freenode"
|
||||
url = "irc://irc.freenode.net/#golang"
|
||||
|
|
|
@ -24,31 +24,27 @@ Without `safeURL`, only the URI schemes `http:`, `https:` and `mailto:` are cons
|
|||
|
||||
The following examples use a [site `config.toml`][configuration] with the following [menu entry][menus]:
|
||||
|
||||
{{% code file="config.toml" copy="false" %}}
|
||||
```toml
|
||||
{{< code file="config.toml" copy="false" >}}
|
||||
[[menu.main]]
|
||||
name = "IRC: #golang at freenode"
|
||||
url = "irc://irc.freenode.net/#golang"
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
The following is an example of a sidebar partial that may be used in conjunction with the preceding front matter example:
|
||||
|
||||
{{% code file="layouts/partials/bad-url-sidebar-menu.html" copy="false" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/bad-url-sidebar-menu.html" copy="false" >}}
|
||||
<!-- This unordered list may be part of a sidebar menu -->
|
||||
<ul>
|
||||
{{ range .Site.Menus.main }}
|
||||
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
This partial would produce the following HTML output:
|
||||
|
||||
{{% output file="bad-url-sidebar-menu-output.html" %}}
|
||||
```html
|
||||
```
|
||||
<!-- This unordered list may be part of a sidebar menu -->
|
||||
<ul>
|
||||
<li><a href="#ZgotmplZ">IRC: #golang at freenode</a></li>
|
||||
|
@ -58,19 +54,17 @@ This partial would produce the following HTML output:
|
|||
|
||||
The odd output can be remedied by adding ` | safeURL` to our `.Title` page variable:
|
||||
|
||||
{{% code file="layouts/partials/correct-url-sidebar-menu.html" copy="false" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/correct-url-sidebar-menu.html" copy="false" >}}
|
||||
<!-- This unordered list may be part of a sidebar menu -->
|
||||
<ul>
|
||||
<li><a href="{{ .URL | safeURL }}">{{ .Name }}</a></li>
|
||||
</ul>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
With the `.URL` page variable piped through `safeURL`, we get the desired output:
|
||||
|
||||
{{% output file="correct-url-sidebar-menu-output.html" %}}
|
||||
```html
|
||||
```
|
||||
<ul class="sidebar-menu">
|
||||
<li><a href="irc://irc.freenode.net/#golang">IRC: #golang at freenode</a></li>
|
||||
</ul>
|
||||
|
|
|
@ -34,14 +34,14 @@ It's named and used in the model of [GNU's seq][].
|
|||
|
||||
You can use `seq` in combination with `range` and `after`. The following will return 19 elements:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ range after 1 (seq 20)}}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
However, when ranging with an index, the following may be less confusing in that `$indexStartingAt1` and `$num` will return `1,2,3 ... 20`:
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ range $index, $num := (seq 20) }}
|
||||
$indexStartingAt1 := (add $index 1)
|
||||
{{ end }}
|
||||
|
|
|
@ -21,14 +21,14 @@ aliases: []
|
|||
|
||||
`sha1` hashes the given input and returns its SHA1 checksum.
|
||||
|
||||
```html
|
||||
```
|
||||
{{ sha1 "Hello world, gophers!" }}
|
||||
<!-- returns the string "c8b5b0e33d408246e30f53e32b8f7627a7a649d4" -->
|
||||
```
|
||||
|
||||
`sha256` hashes the given input and returns its SHA256 checksum.
|
||||
|
||||
```html
|
||||
```
|
||||
{{ sha256 "Hello world, gophers!" }}
|
||||
<!-- returns the string "6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46" -->
|
||||
```
|
||||
|
|
|
@ -20,19 +20,17 @@ draft: false
|
|||
aliases: []
|
||||
---
|
||||
|
||||
{{% code file="shuffle-input.html" %}}
|
||||
```html
|
||||
{{< code file="shuffle-input.html" >}}
|
||||
<!-- Shuffled sequence = -->
|
||||
<div>{{ shuffle (seq 1 5) }}</div>
|
||||
<!-- Shuffled slice = -->
|
||||
<div>{{ shuffle (slice "foo" "bar" "buzz") }}</div>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
This example would return the following:
|
||||
|
||||
{{% output file="shuffle-output.html" %}}
|
||||
```html
|
||||
```
|
||||
<!-- Shuffled sequence = -->
|
||||
<div>2 5 3 1 4</div>
|
||||
<!-- Shuffled slice = -->
|
||||
|
|
|
@ -23,12 +23,10 @@ toc: false
|
|||
|
||||
One use case is the concatenation of elements in combination with the [`delimit` function][]:
|
||||
|
||||
{{% code file="slice.html" %}}
|
||||
```html
|
||||
{{< code file="slice.html" >}}
|
||||
{{ delimit (slice "foo" "bar" "buzz") ", " }}
|
||||
<!-- returns the string "foo, bar, buzz" -->
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
|
||||
[`delimit` function]: /functions/delimit/
|
||||
|
|
|
@ -21,7 +21,7 @@ aliases: []
|
|||
|
||||
A sorted array of map values will be returned with the keys eliminated. There are two optional arguments: `sortByField` and `sortAsc`. If left blank, sort will sort by keys (for maps) in ascending order as its default behavior.
|
||||
|
||||
```toml
|
||||
```
|
||||
+++
|
||||
#tags: [ "tag3", "tag1", "tag2" ]
|
||||
+++
|
||||
|
|
|
@ -33,16 +33,14 @@ The following example takes a UNIX timestamp---set as `utimestamp: "1489276800"`
|
|||
|
||||
The following example may be useful when setting up [multilingual sites][multilingual]:
|
||||
|
||||
{{% code file="unix-to-month-integer.html" %}}
|
||||
```html
|
||||
{{< code file="unix-to-month-integer.html" >}}
|
||||
{{$time := time (int .Params.addDate)}}
|
||||
=> $time = 1489276800
|
||||
{{$time.Month}}
|
||||
=> "March"
|
||||
{{$monthindex := printf "%d" $time.Month }}
|
||||
=> $monthindex = 3
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
|
||||
[int]: /functions/int/
|
||||
|
|
|
@ -21,7 +21,7 @@ aliases: []
|
|||
|
||||
Given two arrays (or slices) A and B, this function will return a new array that contains the elements or objects that belong to either A or to B or to both. The elements supported are strings, integers, and floats (only float64).
|
||||
|
||||
```golang
|
||||
```
|
||||
{{ union (slice 1 2 3) (slice 3 4 5) }}
|
||||
<!-- returns [1 2 3 4 5] -->
|
||||
|
||||
|
@ -38,7 +38,7 @@ Given two arrays (or slices) A and B, this function will return a new array that
|
|||
|
||||
This is also very useful to use as `OR` filters when combined with where:
|
||||
|
||||
```html
|
||||
```
|
||||
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
|
||||
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
|
||||
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
|
||||
|
|
|
@ -20,7 +20,7 @@ aliases: []
|
|||
needsexamples: false
|
||||
---
|
||||
|
||||
```html
|
||||
```
|
||||
{{ uniq (slice 1 2 3 2) }}
|
||||
{{ slice 1 2 3 2 | uniq }}
|
||||
<!-- both return [1 2 3] -->
|
||||
|
|
|
@ -23,11 +23,9 @@ aliases: []
|
|||
|
||||
This very simple one-liner uses `now.Unix` to calculate the amount of time that has passed between the `.LastMod` for the current page and the last build of the current page.
|
||||
|
||||
{{% code file="time-passed.html" %}}
|
||||
```golang
|
||||
{{< code file="time-passed.html" >}}
|
||||
{{ div (sub now.Unix .Lastmod.Unix) 86400 }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
Since both values are integers, they can be subtracted and then divided by the number of seconds in a day (i.e., `60 * 60 * 24 == 86400`).
|
||||
|
||||
|
|
|
@ -20,20 +20,17 @@ relatedfuncs: []
|
|||
|
||||
The following examples pull from a content file with the following front matter:
|
||||
|
||||
{{% code file="content/blog/greatest-city.md" copy="false"%}}
|
||||
```toml
|
||||
{{< code file="content/blog/greatest-city.md" copy="false">}}
|
||||
+++
|
||||
title = "The World's Greatest City"
|
||||
location = "Chicago IL"
|
||||
tags = ["pizza","beer","hot dogs"]
|
||||
+++
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
The following might be used as a partial within a [single page template][singletemplate]:
|
||||
|
||||
{{% code file="layouts/partials/content-header.html" download="content-header.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/content-header.html" download="content-header.html" >}}
|
||||
<header>
|
||||
<h1>{{.Title}}</h1>
|
||||
{{ with .Params.location }}
|
||||
|
@ -50,13 +47,12 @@ The following might be used as a partial within a [single page template][singlet
|
|||
</ul>
|
||||
{{ end }}
|
||||
</header>
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
The preceding partial would then output to the rendered page as follows, assuming the page is being built with Hugo's default pretty URLs.
|
||||
|
||||
{{% output file="/blog/greatest-city/index.html" %}}
|
||||
```html
|
||||
```
|
||||
<header>
|
||||
<h1>The World's Greatest City</h1>
|
||||
<div><a href="/locations/chicago-il/">Chicago IL</a></div>
|
||||
|
|
|
@ -22,7 +22,7 @@ needsexample: true
|
|||
|
||||
`where` filters an array to only the elements containing a matching value for a given field.
|
||||
|
||||
```html
|
||||
```
|
||||
{{ range where .Data.Pages "Section" "post" }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
|
@ -30,13 +30,13 @@ needsexample: true
|
|||
|
||||
It can be used by dot-chaining the second argument to refer to a nested element of a value.
|
||||
|
||||
```toml
|
||||
```
|
||||
+++
|
||||
series: golang
|
||||
+++
|
||||
```
|
||||
|
||||
```html
|
||||
```
|
||||
{{ range where .Site.Pages "Params.series" "golang" }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
|
@ -44,7 +44,7 @@ series: golang
|
|||
|
||||
It can also be used with the logical operators `!=`, `>=`, `in`, etc. Without an operator, `where` compares a given field with a matching value equivalent to `=`.
|
||||
|
||||
```html
|
||||
```
|
||||
{{ range where .Data.Pages "Section" "!=" "post" }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
|
@ -81,7 +81,7 @@ The following logical operators are vailable with `where`:
|
|||
|
||||
## Use `where` with `intersect`
|
||||
|
||||
```html
|
||||
```
|
||||
{{ range where .Site.Pages ".Params.tags" "intersect" .Params.tags }}
|
||||
{{ if ne .Permalink $.Permalink }}
|
||||
{{ .Render "summary" }}
|
||||
|
@ -91,33 +91,29 @@ The following logical operators are vailable with `where`:
|
|||
|
||||
You can also put the returned value of the `where` clauses into a variable:
|
||||
|
||||
{{% code file="where-intersect-variables.html" %}}
|
||||
```html
|
||||
{{< code file="where-intersect-variables.html" >}}
|
||||
{{ $v1 := where .Site.Pages "Params.a" "v1" }}
|
||||
{{ $v2 := where .Site.Pages "Params.b" "v2" }}
|
||||
{{ $filtered := $v1 | intersect $v2 }}
|
||||
{{ range $filtered }}
|
||||
{{ end }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
## Use `where` with `first`
|
||||
|
||||
The following grabs the first five content files in `post` using the [default ordering](/templates/lists/) for lists (i.e., `weight => date`):
|
||||
|
||||
{{% code file="where-with-first.html" %}}
|
||||
```html
|
||||
{{< code file="where-with-first.html" >}}
|
||||
{{ range first 5 (where .Data.Pages "Section" "post") }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
||||
## Nest `where` Clauses
|
||||
|
||||
You can also nest `where` clauses to drill down on lists of content by more than one parameter. The following first grabs all pages in the "blog" section and then ranges through the result of the first `where` clause and finds all pages that are *not* featured:
|
||||
|
||||
```html
|
||||
```
|
||||
{{ range where (where .Data.Pages "Section" "blog" ) ".Params.featured" "!=" "true" }}
|
||||
```
|
||||
|
||||
|
@ -132,7 +128,7 @@ Only the following operators are available for `nil`
|
|||
* `=`, `==`, `eq`: True if the given field is not set.
|
||||
* `!=`, `<>`, `ne`: True if the given field is set.
|
||||
|
||||
```html
|
||||
```
|
||||
{{ range where .Data.Pages ".Params.specialpost" "!=" nil }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
|
|
|
@ -22,12 +22,10 @@ An alternative way of writing an `if` statement and then referencing the same va
|
|||
|
||||
The following example checks for a [user-defined site variable](/variables/site/) called `twitteruser`. If the key-value is not set, the following will render nothing:
|
||||
|
||||
{{% code file="layouts/partials/twitter.html" %}}
|
||||
```html
|
||||
{{< code file="layouts/partials/twitter.html" >}}
|
||||
{{with .Site.Params.twitteruser}}<span class="twitter">
|
||||
<a href="https://twitter.com/{{.}}" rel="author">
|
||||
<img src="/images/twitter.png" width="48" height="48" title="Twitter: {{.}}"
|
||||
alt="Twitter"></a>
|
||||
</span>{{end}}
|
||||
```
|
||||
{{% /code %}}
|
||||
{{< /code >}}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue