mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-15 20:15:49 +00:00
content: Replace calls to the code shortcode with fenced code blocks
This commit is contained in:
1 parent
b23c9a583a
commit
fd628be6ed
110 files changed
+483
-555
No files matched your search
@@ -75,13 +75,13 @@ weight = 20
|
||||
|
||||
With this template code:
|
||||
|
||||
{{< code file=layouts/partials/menu.html >}}
|
||||
```go-html-template {file="layouts/partials/menu.html"}
|
||||
<ul>
|
||||
{{ range .Site.Menus.main }}
|
||||
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo render this HTML:
|
||||
|
||||
@@ -96,13 +96,13 @@ In the above note that the `href` attribute of the second `anchor` element is bl
|
||||
|
||||
With this template code:
|
||||
|
||||
{{< code file=layouts/partials/menu.html >}}
|
||||
```go-html-template {file="layouts/partials/menu.html"}
|
||||
<ul>
|
||||
{{ range .Site.Menus.main }}
|
||||
<li><a href="{{ or .URL .PageRef }}">{{ .Name }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo renders this HTML:
|
||||
|
||||
|
||||
@@ -16,12 +16,12 @@ title = 'How to make spicy tuna hand rolls'
|
||||
description = 'Instructions for making spicy tuna hand rolls.'
|
||||
{{< /code-toggle >}}
|
||||
|
||||
{{< code file=layouts/baseof.html >}}
|
||||
```go-html-template {file="layouts/_default/baseof.html"}
|
||||
<head>
|
||||
...
|
||||
<meta name="description" content="{{ .Description }}">
|
||||
...
|
||||
</head>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
[content summary]: /content-management/summaries/
|
||||
@@ -37,7 +37,7 @@ content/
|
||||
|
||||
The examples below depict the result of rendering works/paintings/the-mona-lisa.md:
|
||||
|
||||
{{< code file=layouts/works/single.html >}}
|
||||
```go-html-template {file="layouts/works/single.html"}
|
||||
{{ with .GetPage "starry-night" }}
|
||||
{{ .Title }} → Starry Night
|
||||
{{ end }}
|
||||
@@ -61,4 +61,4 @@ The examples below depict the result of rendering works/paintings/the-mona-lisa.
|
||||
{{ with .GetPage "/works/sculptures/david" }}
|
||||
{{ .Title }} → David
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
@@ -14,7 +14,7 @@ By example, let's use [Plotly] to render a chart:
|
||||
|
||||
[Plotly]: https://plotly.com/javascript/
|
||||
|
||||
{{< code file=contents/example.md lang=text >}}
|
||||
```text {file="content/example.md"}
|
||||
{{</* plotly */>}}
|
||||
{
|
||||
"data": [
|
||||
@@ -26,21 +26,21 @@ By example, let's use [Plotly] to render a chart:
|
||||
],
|
||||
}
|
||||
{{</* /plotly */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
The shortcode is simple:
|
||||
|
||||
{{< code file=layouts/shortcodes/plotly.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/plotly.html"}
|
||||
{{ $id := printf "plotly-%02d" .Ordinal }}
|
||||
<div id="{{ $id }}"></div>
|
||||
<script>
|
||||
Plotly.newPlot(document.getElementById({{ $id }}), {{ .Inner | safeJS }});
|
||||
</script>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Now we can selectively load the required JavaScript on pages that call the "plotly" shortcode:
|
||||
|
||||
{{< code file=layouts/baseof.html >}}
|
||||
```go-html-template {file="layouts/_default/baseof.html"}
|
||||
<head>
|
||||
...
|
||||
{{ if .HasShortcode "plotly" }}
|
||||
@@ -48,4 +48,4 @@ Now we can selectively load the required JavaScript on pages that call the "plot
|
||||
{{ end }}
|
||||
...
|
||||
</head>
|
||||
{{< /code >}}
|
||||
```
|
||||
@@ -13,8 +13,6 @@ params:
|
||||
|
||||
The `InSection` method on a `Page` object reports whether the given page is in the given section. Note that the method returns `true` when comparing a page to a sibling.
|
||||
|
||||
|
||||
|
||||
With this content structure:
|
||||
|
||||
```text
|
||||
|
||||
@@ -12,21 +12,21 @@ params:
|
||||
|
||||
This is a convenience method, useful within partial templates that are called from both [shortcodes](g) and page templates.
|
||||
|
||||
{{< code file=layouts/shortcodes/foo.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/foo.html"}
|
||||
{{ partial "my-partial.html" . }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
When the shortcode calls the partial, it passes the current [context](g) (the dot). The context includes identifiers such as `Page`, `Params`, `Inner`, and `Name`.
|
||||
|
||||
{{< code file=layouts/_default/single.html >}}
|
||||
```go-html-template {file="layouts/_default/single.html"}
|
||||
{{ partial "my-partial.html" . }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
When the page template calls the partial, it also passes the current context (the dot). But in this case, the dot _is_ the `Page` object.
|
||||
|
||||
{{< code file=layouts/partials/my-partial.html >}}
|
||||
```go-html-template {file="layouts/partials/my-partial.html"}
|
||||
The page title is: {{ .Page.Title }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
To handle both scenarios, the partial template must be able to access the `Page` object with `Page.Page`.
|
||||
|
||||
|
||||
@@ -28,14 +28,14 @@ You can invoke pagination on the [home template], [section templates], [taxonomy
|
||||
[taxonomy templates]: /templates/types/#taxonomy
|
||||
[term templates]: /templates/types/#term
|
||||
|
||||
{{< code file=layouts/_default/list.html >}}
|
||||
```go-html-template {file="layouts/_default/list.html"}
|
||||
{{ $pages := where .Site.RegularPages "Section" "articles" }}
|
||||
{{ $pages = $pages.ByTitle }}
|
||||
{{ range (.Paginate $pages 7).Pages }}
|
||||
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
|
||||
{{ end }}
|
||||
{{ template "_internal/pagination.html" . }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
In the example above, we:
|
||||
|
||||
|
||||
@@ -22,12 +22,12 @@ You can invoke pagination on the [home template], [section templates], [taxonomy
|
||||
[taxonomy templates]: /templates/types/#taxonomy
|
||||
[term templates]: /templates/types/#term
|
||||
|
||||
{{< code file=layouts/_default/list.html >}}
|
||||
```go-html-template {file="layouts/_default/list.html"}
|
||||
{{ range .Paginator.Pages }}
|
||||
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
|
||||
{{ end }}
|
||||
{{ template "_internal/pagination.html" . }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
In the example above, the embedded pagination template creates navigation links between pagers.
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ Use this method in shortcode templates to compose a page from multiple content f
|
||||
|
||||
For example:
|
||||
|
||||
{{< code file=layouts/shortcodes/include.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/include.html" copy=true}
|
||||
{{ with .Get 0 }}
|
||||
{{ with $.Page.GetPage . }}
|
||||
{{- .RenderShortcodes }}
|
||||
@@ -25,15 +25,15 @@ For example:
|
||||
{{ else }}
|
||||
{{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Then call the shortcode in your Markdown:
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{%/* include "/snippets/services" */%}}
|
||||
{{%/* include "/snippets/values" */%}}
|
||||
{{%/* include "/snippets/leadership" */%}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Each of the included Markdown files can contain calls to other shortcodes.
|
||||
|
||||
@@ -50,7 +50,7 @@ Use the latter for the "include" shortcode described above.
|
||||
|
||||
To understand what is returned by the `RenderShortcodes` method, consider this content file
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
+++
|
||||
title = 'About'
|
||||
date = 2023-10-07T12:28:33-07:00
|
||||
@@ -59,7 +59,7 @@ date = 2023-10-07T12:28:33-07:00
|
||||
{{</* ref "privacy" */>}}
|
||||
|
||||
An *emphasized* word.
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
With this template code:
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ changeFreq = 'hourly'
|
||||
|
||||
And this simplistic sitemap template:
|
||||
|
||||
{{< code file=layouts/_default/sitemap.xml >}}
|
||||
```xml {file="layouts/_default/sitemap.xml"}
|
||||
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||
xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
||||
@@ -75,7 +75,7 @@ And this simplistic sitemap template:
|
||||
</url>
|
||||
{{ end }}
|
||||
</urlset>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
The change frequency will be `hourly` for the news page, and `monthly` for other pages.
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ Based on front matter, Hugo uses several factors to identify content related to
|
||||
|
||||
The argument passed to the `Related` method may be a `Page` or an options map. For example, to pass the current page:
|
||||
|
||||
{{< code file=layouts/_default/single.html >}}
|
||||
```go-html-template {file="layouts/_default/single.html"}
|
||||
{{ with .Site.RegularPages.Related . | first 5 }}
|
||||
<p>Related pages:</p>
|
||||
<ul>
|
||||
@@ -24,11 +24,11 @@ The argument passed to the `Related` method may be a `Page` or an options map. F
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
To pass an options map:
|
||||
|
||||
{{< code file=layouts/_default/single.html >}}
|
||||
```go-html-template {file="layouts/_default/single.html"}
|
||||
{{ $opts := dict
|
||||
"document" .
|
||||
"indices" (slice "tags" "keywords")
|
||||
@@ -41,7 +41,7 @@ To pass an options map:
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ The `Content` method on a `Resource` object returns `template.HTML` when the res
|
||||
|
||||
[resource type]: /methods/resource/resourcetype/
|
||||
|
||||
{{< code file=assets/quotations/kipling.txt >}}
|
||||
```text {file="assets/quotations/kipling.txt"}
|
||||
He travels the fastest who travels alone.
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
To get the content:
|
||||
|
||||
|
||||
@@ -37,8 +37,8 @@ content/
|
||||
|
||||
With the structure above, we can range through page resources of type `page` to build content:
|
||||
|
||||
{{< code file=layouts/lessons/single.html >}}
|
||||
```go-html-template {file="layouts/lessons/single.html"}
|
||||
{{ range .Resources.ByType "page" }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
@@ -19,29 +19,29 @@ Some shortcodes support positional arguments, some support named arguments, and
|
||||
|
||||
This shortcode call uses positional arguments:
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{</* myshortcode "Hello" "world" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
To retrieve arguments by position:
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ printf "%s %s." (.Get 0) (.Get 1) }} → Hello world.
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
## Named arguments
|
||||
|
||||
This shortcode call uses named arguments:
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{</* myshortcode greeting="Hello" firstName="world" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
To retrieve arguments by name:
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ printf "%s %s." (.Get "greeting") (.Get "firstName") }} → Hello world.
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< note >}}
|
||||
Argument names are case-sensitive.
|
||||
|
||||
@@ -11,15 +11,15 @@ params:
|
||||
|
||||
This content:
|
||||
|
||||
{{< code file=content/services.md lang=text >}}
|
||||
```text {file="content/services.md"}
|
||||
{{</* card title="Product Design" */>}}
|
||||
We design the **best** widgets in the world.
|
||||
{{</* /card */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
With this shortcode:
|
||||
|
||||
{{< code file=layouts/shortcodes/card.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/card.html"}
|
||||
<div class="card">
|
||||
{{ with .Get "title" }}
|
||||
<div class="card-title">{{ . }}</div>
|
||||
@@ -28,7 +28,7 @@ With this shortcode:
|
||||
{{ .Inner | strings.TrimSpace }}
|
||||
</div>
|
||||
</div>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Is rendered to:
|
||||
|
||||
@@ -57,7 +57,7 @@ Let's modify the example above to pass the value returned by `Inner` through the
|
||||
|
||||
[`RenderString`]: /methods/page/renderstring/
|
||||
|
||||
{{< code file=layouts/shortcodes/card.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/card.html"}
|
||||
<div class="card">
|
||||
{{ with .Get "title" }}
|
||||
<div class="card-title">{{ . }}</div>
|
||||
@@ -66,7 +66,7 @@ Let's modify the example above to pass the value returned by `Inner` through the
|
||||
{{ .Inner | strings.TrimSpace | .Page.RenderString }}
|
||||
</div>
|
||||
</div>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo renders this to:
|
||||
|
||||
@@ -88,11 +88,11 @@ You can use the [`markdownify`] function instead of the `RenderString` method, b
|
||||
|
||||
Instead of calling the shortcode with the `{{</* */>}}` notation, use the `{{%/* */%}}` notation:
|
||||
|
||||
{{< code file=content/services.md lang=text >}}
|
||||
```text {file="content/services.md"}
|
||||
{{%/* card title="Product Design" */%}}
|
||||
We design the **best** widgets in the world.
|
||||
{{%/* /card */%}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
When you use the `{{%/* */%}}` notation, Hugo renders the entire shortcode as Markdown, requiring the following changes.
|
||||
|
||||
@@ -107,7 +107,7 @@ This configuration is not unsafe if _you_ control the content. Read more about H
|
||||
|
||||
Second, because we are rendering the entire shortcode as Markdown, we must adhere to the rules governing [indentation] and inclusion of [raw HTML blocks] as provided in the [CommonMark] specification.
|
||||
|
||||
{{< code file=layouts/shortcodes/card.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/card.html"}
|
||||
<div class="card">
|
||||
{{ with .Get "title" }}
|
||||
<div class="card-title">{{ . }}</div>
|
||||
@@ -117,7 +117,7 @@ Second, because we are rendering the entire shortcode as Markdown, we must adher
|
||||
{{ .Inner | strings.TrimSpace }}
|
||||
</div>
|
||||
</div>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
The difference between this and the previous example is subtle but required. Note the change in indentation, the addition of a blank line, and removal of the `RenderString` method.
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ This allows us to effectively bypass the rules governing [indentation] as provid
|
||||
|
||||
Consider this Markdown, an unordered list with a small gallery of thumbnail images within each list item:
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
- Gallery one
|
||||
|
||||
{{</* gallery */>}}
|
||||
@@ -29,17 +29,17 @@ Consider this Markdown, an unordered list with a small gallery of thumbnail imag
|
||||

|
||||

|
||||
{{</* /gallery */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
In the example above, notice that the content between the opening and closing shortcode tags is indented by four spaces. Per the CommonMark specification, this is treated as an indented code block.
|
||||
|
||||
With this shortcode, calling `Inner` instead of `InnerDeindent`:
|
||||
|
||||
{{< code file=layouts/shortcodes/gallery.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/gallery.html"}
|
||||
<div class="gallery">
|
||||
{{ .Inner | strings.TrimSpace | .Page.RenderString }}
|
||||
</div>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo renders the Markdown to:
|
||||
|
||||
@@ -66,11 +66,11 @@ Hugo renders the Markdown to:
|
||||
|
||||
Although technically correct per the CommonMark specification, this is not what we want. If we remove the indentation using the `InnerDeindent` method:
|
||||
|
||||
{{< code file=layouts/shortcodes/gallery.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/gallery.html"}
|
||||
<div class="gallery">
|
||||
{{ .InnerDeindent | strings.TrimSpace | .Page.RenderString }}
|
||||
</div>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo renders the Markdown to:
|
||||
|
||||
|
||||
@@ -13,17 +13,17 @@ To support both positional and named arguments when calling a shortcode, use the
|
||||
|
||||
With this shortcode template:
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ if .IsNamedParams }}
|
||||
{{ printf "%s %s." (.Get "greeting") (.Get "firstName") }}
|
||||
{{ else }}
|
||||
{{ printf "%s %s." (.Get 0) (.Get 1) }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Both of these calls return the same value:
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{</* myshortcode greeting="Hello" firstName="world" */>}}
|
||||
{{</* myshortcode "Hello" "world" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
@@ -11,14 +11,14 @@ params:
|
||||
|
||||
The `Name` method is useful for error reporting. For example, if your shortcode requires a "greeting" argument:
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ $greeting := "" }}
|
||||
{{ with .Get "greeting" }}
|
||||
{{ $greeting = . }}
|
||||
{{ else }}
|
||||
{{ errorf "The %q shortcode requires a 'greeting' argument. See %s" .Name .Position }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
In the absence of a "greeting" argument, Hugo will throw an error message and fail the build:
|
||||
|
||||
|
||||
@@ -18,15 +18,15 @@ Hugo increments the ordinal with each shortcode call, regardless of the specific
|
||||
|
||||
This method is useful for, among other things, assigning unique element IDs when a shortcode is called two or more times from the same page. For example:
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{</* img src="images/a.jpg" */>}}
|
||||
|
||||
{{</* img src="images/b.jpg" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
This shortcode performs error checking, then renders an HTML `img` element with a unique `id` attribute:
|
||||
|
||||
{{< code file=layouts/shortcodes/img.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/img.html"}
|
||||
{{ $src := "" }}
|
||||
{{ with .Get "src" }}
|
||||
{{ $src = . }}
|
||||
@@ -39,7 +39,7 @@ This shortcode performs error checking, then renders an HTML `img` element with
|
||||
{{ else }}
|
||||
{{ errorf "The %q shortcode requires a 'src' argument. See %s" .Name .Position }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Hugo renders the page to:
|
||||
|
||||
|
||||
@@ -27,11 +27,11 @@ Calling this shortcode:
|
||||
|
||||
We can access the front matter values using the `Page` method:
|
||||
|
||||
{{< code file=layouts/shortcodes/book-details.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/book-details.html"}
|
||||
<ul>
|
||||
<li>Title: {{ .Page.Title }}</li>
|
||||
<li>Author: {{ .Page.Params.author }}</li>
|
||||
<li>Published: {{ .Page.Params.publication_year }}</li>
|
||||
<li>ISBN: {{ .Page.Params.isbn }}</li>
|
||||
</ul>
|
||||
{{< /code >}}
|
||||
```
|
||||
@@ -11,22 +11,22 @@ params:
|
||||
|
||||
When you call a shortcode using positional arguments, the `Params` method returns a slice.
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{</* myshortcode "Hello" "world" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ index .Params 0 }} → Hello
|
||||
{{ index .Params 1 }} → world
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
When you call a shortcode using named arguments, the `Params` method returns a map.
|
||||
|
||||
{{< code file=content/about.md lang=text >}}
|
||||
```text {file="content/about.md"}
|
||||
{{</* myshortcode greeting="Hello" name="world" */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ .Params.greeting }} → Hello
|
||||
{{ .Params.name }} → world
|
||||
{{< /code >}}
|
||||
```
|
||||
@@ -14,19 +14,19 @@ This is useful for inheritance of common shortcode arguments from the root.
|
||||
|
||||
In this contrived example, the "greeting" shortcode is the parent, and the "now" shortcode is child.
|
||||
|
||||
{{< code file=content/welcome.md lang=text >}}
|
||||
```text {file="content/welcome.md"}
|
||||
{{</* greeting dateFormat="Jan 2, 2006" */>}}
|
||||
Welcome. Today is {{</* now */>}}.
|
||||
{{</* /greeting */>}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=layouts/shortcodes/greeting.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/greeting.html"}
|
||||
<div class="greeting">
|
||||
{{ .Inner | strings.TrimSpace | .Page.RenderString }}
|
||||
</div>
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=layouts/shortcodes/now.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/now.html"}
|
||||
{{- $dateFormat := "January 2, 2006 15:04:05" }}
|
||||
|
||||
{{- with .Params }}
|
||||
@@ -42,7 +42,7 @@ Welcome. Today is {{</* now */>}}.
|
||||
{{- end }}
|
||||
|
||||
{{- now | time.Format $dateFormat -}}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
The "now" shortcode formats the current time using:
|
||||
|
||||
|
||||
@@ -11,14 +11,14 @@ params:
|
||||
|
||||
The `Position` method is useful for error reporting. For example, if your shortcode requires a "greeting" argument:
|
||||
|
||||
{{< code file=layouts/shortcodes/myshortcode.html >}}
|
||||
```go-html-template {file="layouts/shortcodes/myshortcode.html"}
|
||||
{{ $greeting := "" }}
|
||||
{{ with .Get "greeting" }}
|
||||
{{ $greeting = . }}
|
||||
{{ else }}
|
||||
{{ errorf "The %q shortcode requires a 'greeting' argument. See %s" .Name .Position }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
In the absence of a "greeting" argument, Hugo will throw an error message and fail the build:
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ params:
|
||||
signatures: [SHORTCODE.Ref OPTIONS]
|
||||
---
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
The `Ref` method accepts a single argument: an options map.
|
||||
|
||||
@@ -33,23 +33,23 @@ data/
|
||||
|
||||
And these data files:
|
||||
|
||||
{{< code file=data/books/fiction.yaml lang=yaml >}}
|
||||
```yaml {file="data/books/fiction.yaml"}
|
||||
- title: The Hunchback of Notre Dame
|
||||
author: Victor Hugo
|
||||
isbn: 978-0140443530
|
||||
- title: Les Misérables
|
||||
author: Victor Hugo
|
||||
isbn: 978-0451419439
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
{{< code file=data/books/nonfiction.yaml lang=yaml >}}
|
||||
```yaml {file="data/books/nonfiction.yaml"}
|
||||
- title: The Ancien Régime and the Revolution
|
||||
author: Alexis de Tocqueville
|
||||
isbn: 978-0141441641
|
||||
- title: Interpreting the French Revolution
|
||||
author: François Furet
|
||||
isbn: 978-0521280495
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
Access the data by [chaining](g) the [identifiers](g):
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@ params:
|
||||
|
||||
This method returns all page [kinds](g) in the current language, in the [default sort order](g). That includes the home page, section pages, taxonomy pages, term pages, and regular pages.
|
||||
|
||||
|
||||
|
||||
In most cases you should use the [`RegularPages`] method instead.
|
||||
|
||||
[`RegularPages`]: /methods/site/regularpages/
|
||||
|
||||
@@ -144,7 +144,7 @@ The following example displays all terms in a site's tags taxonomy:
|
||||
```
|
||||
This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.
|
||||
|
||||
{{< code file=layouts/partials/all-taxonomies.html >}}
|
||||
```go-html-template {file="layouts/partials/all-taxonomies.html"}
|
||||
{{ with .Site.Taxonomies }}
|
||||
{{ $numberOfTerms := 0 }}
|
||||
{{ range $taxonomy, $terms := . }}
|
||||
@@ -175,4 +175,4 @@ This example will list all taxonomies and their terms, as well as all the conten
|
||||
</ul>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
Reference in new issue
Block a user