summaryrefslogtreecommitdiffstats
path: root/docs/content/en/troubleshooting/inspection.md
blob: 8262291536102c42bbb96719565c5401dfcc17c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: Data inspection
linkTitle: Inspection
description: Use template functions to inspect values and data structures.
categories: [troubleshooting]
keywords: []
menu:
  docs:
    parent: troubleshooting
    weight: 40
weight: 40
---

Use the [`jsonify`] function to inspect a data structure:

```go-html-template
<pre>{{ jsonify (dict "indent" "  ") .Params }}</pre>
```

```text
{
  "date": "2023-11-10T15:10:42-08:00",
  "draft": false,
  "iscjklanguage": false,
  "lastmod": "2023-11-10T15:10:42-08:00",
  "publishdate": "2023-11-10T15:10:42-08:00",
  "tags": [
    "foo",
    "bar"
  ],
  "title": "My first post"
}
```

{{% 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
{{ $value := 42 }}
{{ 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