← Back Home

The Most Useful Hugo Theme Partial

  1. static website
  2. hugo
  3. partials

If you’re building a new Hugo theme, this is the most useful partial for debugging purposes. It will show the page variables available to you on any given page.

Output

Hugo Variables for the current page.
Variable Value
.Name "The Most Useful Hugo Theme Partial"
.Title "The Most Useful Hugo Theme Partial"
.Kind "page"
.Type "posts"
.IsPage true
.IsHome false
.Next
.Prev Page(/examples/clare-example-section.md)
.Params
"date": time.Time{wall:0x0, ext:63733803643, loc:(*time.Location)(0x6e24420)}
"draft": false
"iscjklanguage": false
"lastmod": time.Time{wall:0x0, ext:63733803643, loc:(*time.Location)(0x6e24420)}
"publishdate": time.Time{wall:0x0, ext:63733803643, loc:(*time.Location)(0x6e24420)}
"tags": []string{"static website", "hugo", "partials"}
"title": "The Most Useful Hugo Theme Partial"
.Section "posts"
.CurrentSection Page(/posts)
.Pages
.Pages Pages(0)
.Resources []
.File posts/useful-hugo-theme-partial.md
.File.Dir "posts/"

HTML

<!-- themes/your-theme/layouts/partials/hugo-variables.html -->
<div class="h-40 mb-4 py-4 px-4 overflow-y-auto scrolling-touch border-t border-gray-200">
    <table class="w-full text-left table-auto table-collapse border-gray-200">
        <caption class="text-left pb-2">Hugo Variables for the current page.</caption>
        <thead>
            <tr class="p-2 font-semibold bg-gray-100 border-t ">
                <th>Variable</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
            <tr class="p-2 border-t">
                <td>.Name</td>
                <td>{{ printf "%#v" .Name }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Title</td>
                <td>{{ printf "%#v" .Title }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Kind</td>
                <td>{{ printf "%#v" .Kind }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Type</td>
                <td>{{ printf "%#v" .Type }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.IsPage</td>
                <td>{{ printf "%#v" .IsPage }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.IsHome</td>
                <td>{{ printf "%#v" .IsHome }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Next</td>
                <td>{{ printf "%v" .Next }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Prev</td>
                <td>{{ printf "%v" .Prev }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Params</td>
                <td>
                    <table>
                        {{ range $k,$v := .Params }}
                        <tr>
                            <td class="text-right italic pr-2">{{ printf "%#v:" $k }}</td>
                            <td>{{ printf "%#v" $v }}</td>
                        </tr>
                        {{ end }}
                    </table>
                </td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Section</td>
                <td>{{ printf "%#v" .Section }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.CurrentSection</td>
                <td>{{ printf "%v" .CurrentSection }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Pages</td>
                <td>
                    {{ printf "%v" .Pages }}
                    <table>
                        {{- range $k,$v := .Pages }}
                        <tr>
                            <td class="text-right italic pr-2">{{ printf "%#v:" $k }}</td>
                            <td>{{ printf "%v" $v }}</td>
                        </tr>
                        {{ end -}}
                    </table>
                </td>
            </tr>
            <tr class="p-2 border-t">
                <td>.Resources</td>
                <td>{{ printf "%v" .Resources }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.File</td>
                <td>{{ if .File }}{{ printf "%v" .File }}{{ else}}""{{ end }}</td>
            </tr>
            <tr class="p-2 border-t">
                <td>.File.Dir</td>
                <td>{{ if .File }}{{ printf "%#v" .File.Dir }}{{ else}}""{{ end }}</td>
            </tr>
        </tbody>
    </table>
</div>

Final

Now you can use your partial wherever you want. I keep mine in my _baseof.html

<!DOCTYPE html>
<html lang='{{ .Site.LanguageCode }}'>
    {{ partial "head.html" . }}
    <body>
        {{- partial "header.html" . -}}
        {{- block "main" . }}{{- end }}
        {{- partial "hugo-variables.html" . -}}
        {{- partial "footer.html" . -}}
    </body>
</html>

Subscribe to my newsletter!