Update debug.Dump

This commit is contained in:
Joe Mooring 2024-04-16 08:42:00 -07:00 committed by GitHub
parent 38d6731e29
commit 3dcf32e25b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 24 additions and 63 deletions

View File

@ -11,33 +11,22 @@ action:
--- ---
```go-html-template ```go-html-template
{{ $data := dict }} <pre>{{ debug.Dump site.Data.books }}</pre>
{{ $p := "data/books.json" }}
{{ with resources.Get $p }}
{{ $opts := dict "delimiter" "," }}
{{ $data = .Content | transform.Unmarshal $opts }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
``` ```
```go-html-template ```json
<pre>{{ debug.Dump $data }}</pre> [
``` {
```text
[]interface {}{
map[string]interface {}{
"author": "Victor Hugo", "author": "Victor Hugo",
"rating": 5.0, "rating": 4,
"title": "Les Misérables", "title": "The Hunchback of Notre Dame"
}, },
map[string]interface {}{ {
"author": "Victor Hugo", "author": "Victor Hugo",
"rating": 4.0, "rating": 5,
"title": "The Hunchback of Notre Dame", "title": "Les Misérables"
}, }
} ]
``` ```
{{% note %}} {{% note %}}

View File

@ -182,7 +182,7 @@ Get the remote data:
Inspect the data structure: Inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") $data }}</pre> <pre>{{ debug.Dump $data }}</pre>
``` ```
List the book titles: List the book titles:
@ -245,7 +245,7 @@ Let's add a `lang` attribute to the `title` nodes of our RSS feed, and a namespa
After retrieving the remote data, inspect the data structure: After retrieving the remote data, inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") $data }}</pre> <pre>{{ debug.Dump $data }}</pre>
``` ```
Each item node looks like this: Each item node looks like this:

View File

@ -31,21 +31,21 @@ Headings
: (`map`) A nested map of all headings on the page. Each map contains the following keys: `ID`, `Level`, `Title` and `Headings`. To inspect the data structure: : (`map`) A nested map of all headings on the page. Each map contains the following keys: `ID`, `Level`, `Title` and `Headings`. To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ .Fragments.Headings | jsonify (dict "indent" " ") }}</pre> <pre>{{ debug.Dump .Fragments.Headings }}</pre>
``` ```
HeadingsMap HeadingsMap
: (`slice`) A slice of maps of all headings on the page, with first-level keys for each heading. Each map contains the following keys: `ID`, `Level`, `Title` and `Headings`. To inspect the data structure: : (`slice`) A slice of maps of all headings on the page, with first-level keys for each heading. Each map contains the following keys: `ID`, `Level`, `Title` and `Headings`. To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ .Fragments.HeadingsMap | jsonify (dict "indent" " ") }}</pre> <pre>{{ debug.Dump .Fragments.HeadingsMap }}</pre>
``` ```
Identifiers Identifiers
: (`slice`) A slice containing the `id` of each heading on the page. To inspect the data structure: : (`slice`) A slice containing the `id` of each heading on the page. To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ .Fragments.Identifiers | jsonify (dict "indent" " ") }}</pre> <pre>{{ debug.Dump .Fragments.Identifiers }}</pre>
``` ```
Identifiers.Contains ID Identifiers.Contains ID

View File

@ -12,10 +12,10 @@ action:
The `Languages` method on a `Site` object returns a collection of language objects for all sites, ordered by language weight. Each language object points to its language definition in the site configuration. The `Languages` method on a `Site` object returns a collection of language objects for all sites, ordered by language weight. Each language object points to its language definition in the site configuration.
To view the data structure: To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") .Site.Languages }}</pre> <pre>{{ debug.Dump .Site.Languages }}</pre>
``` ```
With this site configuration: With this site configuration:

View File

@ -34,7 +34,7 @@ To reverse the sort order:
To inspect the data structure: To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") $taxonomyObject.Alphabetical }}</pre> <pre>{{ debug.Dump $taxonomyObject.Alphabetical }}</pre>
``` ```
{{% include "methods/taxonomy/_common/ordered-taxonomy-element-methods.md" %}} {{% include "methods/taxonomy/_common/ordered-taxonomy-element-methods.md" %}}

View File

@ -34,7 +34,7 @@ To reverse the sort order:
To inspect the data structure: To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") $taxonomyObject.ByCount }}</pre> <pre>{{ debug.Dump $taxonomyObject.ByCount }}</pre>
``` ```
{{% include "methods/taxonomy/_common/ordered-taxonomy-element-methods.md" %}} {{% include "methods/taxonomy/_common/ordered-taxonomy-element-methods.md" %}}

View File

@ -43,7 +43,7 @@ You could also use the [`index`] function, but the syntax is more verbose:
To inspect the data structure: To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") $weightedPages }}</pre> <pre>{{ debug.Dump $weightedPages }}</pre>
``` ```
## Example ## Example

View File

@ -41,7 +41,7 @@ To capture the "genres" taxonomy object when rendering its page with a taxonomy
To inspect the data structure: To inspect the data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") $taxonomyObject }}</pre> <pre>{{ debug.Dump $taxonomyObject }}</pre>
``` ```
Although the [`Alphabetical`] and [`ByCount`] methods provide a better data structure for ranging through the taxonomy, you can render the weighted pages by term directly from the `Taxonomy` object: Although the [`Alphabetical`] and [`ByCount`] methods provide a better data structure for ranging through the taxonomy, you can render the weighted pages by term directly from the `Taxonomy` object:

View File

@ -11,10 +11,10 @@ menu:
weight: 40 weight: 40
--- ---
Use the [`jsonify`] function to inspect a data structure: Use the [`debug.Dump`] function to inspect a data structure:
```go-html-template ```go-html-template
<pre>{{ jsonify (dict "indent" " ") .Params }}</pre> <pre>{{ debug.Dump .Params }}</pre>
``` ```
```text ```text
@ -32,33 +32,6 @@ Use the [`jsonify`] function to inspect a data structure:
} }
``` ```
{{% note %}}
Hugo will throw an error if you attempt to use the construct above to display context that includes a page collection. For example, in a home page template, this will fail:
`{{ jsonify (dict "indent" " ") . }}`
{{% /note %}}
Use the [`debug.Dump`] function to inspect data types:
```go-html-template
<pre>{{ debug.Dump .Params }}</pre>
```
```text
maps.Params{
"date": time.Time{},
"draft": false,
"iscjklanguage": false,
"lastmod": time.Time{},
"publishdate": time.Time{},
"tags": []string{
"foo",
"bar",
},
"title": "My first post",
}
```
Use the [`printf`] function (render) or [`warnf`] function (log to console) to inspect simple data structures. The layout string below displays both value and data type. Use the [`printf`] function (render) or [`warnf`] function (log to console) to inspect simple data structures. The layout string below displays both value and data type.
```go-html-template ```go-html-template
@ -66,7 +39,6 @@ Use the [`printf`] function (render) or [`warnf`] function (log to console) to i
{{ printf "%[1]v (%[1]T)" $value }} → 42 (int) {{ printf "%[1]v (%[1]T)" $value }} → 42 (int)
``` ```
[`jsonify`]: /functions/encoding/jsonify/
[`debug.Dump`]: /functions/debug/dump/ [`debug.Dump`]: /functions/debug/dump/
[`printf`]: /functions/fmt/printf/ [`printf`]: /functions/fmt/printf/
[`warnf`]: /functions/fmt/warnf/ [`warnf`]: /functions/fmt/warnf/