mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-09 09:05:11 -04:00
Update debug.Dump
This commit is contained in:
parent
38d6731e29
commit
3dcf32e25b
@ -11,33 +11,22 @@ action:
|
||||
---
|
||||
|
||||
```go-html-template
|
||||
{{ $data := dict }}
|
||||
{{ $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 }}
|
||||
<pre>{{ debug.Dump site.Data.books }}</pre>
|
||||
```
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ debug.Dump $data }}</pre>
|
||||
```
|
||||
|
||||
```text
|
||||
[]interface {}{
|
||||
map[string]interface {}{
|
||||
```json
|
||||
[
|
||||
{
|
||||
"author": "Victor Hugo",
|
||||
"rating": 5.0,
|
||||
"title": "Les Misérables",
|
||||
"rating": 4,
|
||||
"title": "The Hunchback of Notre Dame"
|
||||
},
|
||||
map[string]interface {}{
|
||||
{
|
||||
"author": "Victor Hugo",
|
||||
"rating": 4.0,
|
||||
"title": "The Hunchback of Notre Dame",
|
||||
},
|
||||
}
|
||||
"rating": 5,
|
||||
"title": "Les Misérables"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
|
@ -182,7 +182,7 @@ Get the remote data:
|
||||
Inspect the data structure:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ jsonify (dict "indent" " ") $data }}</pre>
|
||||
<pre>{{ debug.Dump $data }}</pre>
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ jsonify (dict "indent" " ") $data }}</pre>
|
||||
<pre>{{ debug.Dump $data }}</pre>
|
||||
```
|
||||
|
||||
Each item node looks like this:
|
||||
|
@ -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:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ .Fragments.Headings | jsonify (dict "indent" " ") }}</pre>
|
||||
<pre>{{ debug.Dump .Fragments.Headings }}</pre>
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ .Fragments.HeadingsMap | jsonify (dict "indent" " ") }}</pre>
|
||||
<pre>{{ debug.Dump .Fragments.HeadingsMap }}</pre>
|
||||
```
|
||||
|
||||
Identifiers
|
||||
: (`slice`) A slice containing the `id` of each heading on the page. To inspect the data structure:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ .Fragments.Identifiers | jsonify (dict "indent" " ") }}</pre>
|
||||
<pre>{{ debug.Dump .Fragments.Identifiers }}</pre>
|
||||
```
|
||||
|
||||
Identifiers.Contains ID
|
||||
|
@ -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.
|
||||
|
||||
To view the data structure:
|
||||
To inspect the data structure:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ jsonify (dict "indent" " ") .Site.Languages }}</pre>
|
||||
<pre>{{ debug.Dump .Site.Languages }}</pre>
|
||||
```
|
||||
|
||||
With this site configuration:
|
||||
|
@ -34,7 +34,7 @@ To reverse the sort order:
|
||||
To inspect the data structure:
|
||||
|
||||
```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" %}}
|
||||
|
@ -34,7 +34,7 @@ To reverse the sort order:
|
||||
To inspect the data structure:
|
||||
|
||||
```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" %}}
|
||||
|
@ -43,7 +43,7 @@ You could also use the [`index`] function, but the syntax is more verbose:
|
||||
To inspect the data structure:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ jsonify (dict "indent" " ") $weightedPages }}</pre>
|
||||
<pre>{{ debug.Dump $weightedPages }}</pre>
|
||||
```
|
||||
|
||||
## Example
|
||||
|
@ -41,7 +41,7 @@ To capture the "genres" taxonomy object when rendering its page with a taxonomy
|
||||
To inspect the data structure:
|
||||
|
||||
```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:
|
||||
|
@ -11,10 +11,10 @@ menu:
|
||||
weight: 40
|
||||
---
|
||||
|
||||
Use the [`jsonify`] function to inspect a data structure:
|
||||
Use the [`debug.Dump`] function to inspect a data structure:
|
||||
|
||||
```go-html-template
|
||||
<pre>{{ jsonify (dict "indent" " ") .Params }}</pre>
|
||||
<pre>{{ debug.Dump .Params }}</pre>
|
||||
```
|
||||
|
||||
```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.
|
||||
|
||||
```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)
|
||||
```
|
||||
|
||||
[`jsonify`]: /functions/encoding/jsonify/
|
||||
[`debug.Dump`]: /functions/debug/dump/
|
||||
[`printf`]: /functions/fmt/printf/
|
||||
[`warnf`]: /functions/fmt/warnf/
|
||||
|
Loading…
x
Reference in New Issue
Block a user