mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
Merge commit '7eb0e10a80708c638554b8221a3120dc1168566c'
This commit is contained in:
@@ -34,6 +34,8 @@ Layout
|
||||
Output Format
|
||||
: See [Custom Output Formats](/templates/output-formats). An output format has both a `name` (e.g. `rss`, `amp`, `html`) and a `suffix` (e.g. `xml`, `html`). We prefer matches with both (e.g. `index.amp.html`, but look for less specific templates.
|
||||
|
||||
Note that if the output format's Media Type has more than one suffix defined, only the first is considered.
|
||||
|
||||
Language
|
||||
: We will consider a language code in the template name. If the site language is `fr`, `index.fr.amp.html` will win over `index.amp.html`, but `index.amp.html` will be chosen before `index.fr.html`.
|
||||
|
||||
@@ -79,7 +81,3 @@ In Hugo, layouts can live in either the project's or the themes' layout folders,
|
||||
## Examples: Layout Lookup for Term Pages
|
||||
|
||||
{{< datatable-filtered "output" "layouts" "Kind == term" "Example" "OutputFormat" "Suffix" "Template Lookup Order" >}}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -88,21 +88,23 @@ For the second position, you would just use:
|
||||
`with` is great when the output depends on a parameter being set:
|
||||
|
||||
```
|
||||
{{ with .Get "class"}} class="{{.}}"{{ end }}
|
||||
{{ with .Get "class" }} class="{{ . }}"{{ end }}
|
||||
```
|
||||
|
||||
`.Get` can also be used to check if a parameter has been provided. This is
|
||||
most helpful when the condition depends on either of the values, or both:
|
||||
|
||||
```
|
||||
{{ if or (.Get "title") (.Get "alt") }} alt="{{ with .Get "alt"}}{{.}}{{else}}{{.Get "title"}}{{end}}"{{ end }}
|
||||
{{ if or (.Get "title") (.Get "alt") }} alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "title" }}{{ end }}"{{ end }}
|
||||
```
|
||||
|
||||
#### `.Inner`
|
||||
|
||||
If a closing shortcode is used, the `.Inner` variable will be populated with all of the content between the opening and closing shortcodes. If a closing shortcode is required, you can check the length of `.Inner` as an indicator of its existence.
|
||||
|
||||
A shortcode with content declared via the `.Inner` variable can also be declared without the inline content and without the closing shortcode by using the self-closing syntax:
|
||||
A shortcode with content declared via the `.Inner` variable can also be declared without the
|
||||
content and without the closing
|
||||
by using the self-closing syntax:
|
||||
|
||||
```
|
||||
{{</* innershortcode /*/>}}
|
||||
@@ -128,16 +130,16 @@ The `.IsNamedParams` variable checks whether the shortcode declaration uses name
|
||||
For example, you could create an `image` shortcode that can take either a `src` named parameter or the first positional parameter, depending on the preference of the content's author. Let's assume the `image` shortcode is called as follows:
|
||||
|
||||
```
|
||||
{{</* image src="images/my-image.jpg"*/>}}
|
||||
{{</* image src="images/my-image.jpg" */>}}
|
||||
```
|
||||
|
||||
You could then include the following as part of your shortcode templating:
|
||||
|
||||
```
|
||||
{{ if .IsNamedParams }}
|
||||
<img src="{{.Get "src" }}" alt="">
|
||||
<img src="{{ .Get "src" }}" alt="">
|
||||
{{ else }}
|
||||
<img src="{{.Get 0}}" alt="">
|
||||
<img src="{{ .Get 0 }}" alt="">
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -211,17 +213,17 @@ You have created the shortcode at `/layouts/shortcodes/img.html`, which loads th
|
||||
{{< code file="/layouts/shortcodes/img.html" >}}
|
||||
<!-- image -->
|
||||
<figure {{ with .Get "class" }}class="{{.}}"{{ end }}>
|
||||
{{ with .Get "link"}}<a href="{{.}}">{{ end }}
|
||||
<img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}"{{ end }} />
|
||||
{{ if .Get "link"}}</a>{{ end }}
|
||||
{{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
|
||||
{{ with .Get "link" }}<a href="{{ . }}">{{ end }}
|
||||
<img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "caption" }}{{ end }}"{{ end }} />
|
||||
{{ if .Get "link" }}</a>{{ end }}
|
||||
{{ if or (or (.Get "title") (.Get "caption")) (.Get "attr") }}
|
||||
<figcaption>{{ if isset .Params "title" }}
|
||||
<h4>{{ .Get "title" }}</h4>{{ end }}
|
||||
{{ if or (.Get "caption") (.Get "attr")}}<p>
|
||||
{{ if or (.Get "caption") (.Get "attr") }}<p>
|
||||
{{ .Get "caption" }}
|
||||
{{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}
|
||||
{{ with .Get "attrlink" }}<a href="{{ . }}"> {{ end }}
|
||||
{{ .Get "attr" }}
|
||||
{{ if .Get "attrlink"}}</a> {{ end }}
|
||||
{{ if .Get "attrlink" }}</a> {{ end }}
|
||||
</p> {{ end }}
|
||||
</figcaption>
|
||||
{{ end }}
|
||||
@@ -287,7 +289,7 @@ The following is taken from `highlight`, which is a [built-in shortcode][] that
|
||||
The template for the `highlight` shortcode uses the following code, which is already included in Hugo:
|
||||
|
||||
```
|
||||
{{ .Get 0 | highlight .Inner }}
|
||||
{{ .Get 0 | highlight .Inner }}
|
||||
```
|
||||
|
||||
The rendered output of the HTML example code block will be as follows:
|
||||
@@ -306,8 +308,8 @@ Hugo's [`.Parent` shortcode variable][parent] returns a boolean value depending
|
||||
The following example is contrived but demonstrates the concept. Assume you have a `gallery` shortcode that expects one named `class` parameter:
|
||||
|
||||
{{< code file="layouts/shortcodes/gallery.html" >}}
|
||||
<div class="{{.Get "class"}}">
|
||||
{{.Inner}}
|
||||
<div class="{{ .Get "class" }}">
|
||||
{{ .Inner }}
|
||||
</div>
|
||||
{{< /code >}}
|
||||
|
||||
@@ -316,10 +318,10 @@ You also have an `img` shortcode with a single named `src` parameter that you wa
|
||||
{{< code file="layouts/shortcodes/img.html" >}}
|
||||
{{- $src := .Get "src" -}}
|
||||
{{- with .Parent -}}
|
||||
<img src="{{$src}}" class="{{.Get "class"}}-image">
|
||||
<img src="{{$src}}" class="{{ .Get "class" }}-image">
|
||||
{{- else -}}
|
||||
<img src="{{$src}}">
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
{{< /code >}}
|
||||
|
||||
You can then call your shortcode in your content as follows:
|
||||
@@ -367,6 +369,8 @@ More shortcode examples can be found in the [shortcodes directory for spf13.com]
|
||||
|
||||
## Inline Shortcodes
|
||||
|
||||
{{< new-in "0.52.0" >}}
|
||||
|
||||
Since Hugo 0.52, you can implement your shortcodes inline -- e.g. where you use them in the content file. This can be useful for scripting that you only need in one place.
|
||||
|
||||
This feature is disabled by default, but can be enabled in your site config:
|
||||
|
Reference in New Issue
Block a user