hugo/content/templates/template-debugging.md
Bjørn Erik Pedersen ba45da9d03 Squashed 'docs/' changes from 44fe0285..32356e4e
32356e4e Fix typo in header of shortcode-templates.md
c8f1a2d2 Correct code example for index template function
bfa6a55d Escape code fencing
ff8b2f99 Fix typos in deployment with wercker tutorial
557c36e8 theme: Merge commit '7fbb4bed25001182bfeb91f79db0f0c1936582ee'
7fbb4bed Squashed 'themes/gohugoioTheme/' changes from 7dd8a302..ca53082d
ce31cee0 Add "See Also" config
158cee1b Make the tags into keywords
61600be6 Add a note to the related section
49edb5a2 Relase 0.27.1
c9bbc001 releaser: Add release notes to /docs for release of 0.27.1
213c6c3b Add bugs poster
8b4590cd Add KeyCDN integration tutorial
2b277859 Add tutorial videos to several docs pages
950fef1f Update roadmap to link to the correct milestones page
496f5bf6 Rename relnotes
d6f9378d Bump Netlify versions to 0.27
087fde7f Update 0.27 release notes
603f94ae docs: Document Related Content
3790f6a3 releaser: Bump versions for release of 0.27
0948868c releaser: Add release notes to /docs for release of 0.27

git-subtree-dir: docs
git-subtree-split: 32356e4eabe357ae914f4d1d59e8ae31ce936723
2017-09-21 19:03:00 +02:00

81 lines
No EOL
2.2 KiB
Markdown

---
title: Template Debugging
# linktitle: Template Debugging
description: You can use Go templates' `printf` function to debug your Hugo templates. These snippets provide a quick and easy visualization of the variables available to you in different contexts.
godocref: http://golang.org/pkg/fmt/
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
categories: [templates]
keywords: [debugging,troubleshooting]
menu:
docs:
parent: "templates"
weight: 180
weight: 180
sections_weight: 180
draft: false
aliases: []
toc: false
---
Here are some snippets you can add to your template to answer some common questions.
These snippets use the `printf` function available in all Go templates. This function is an alias to the Go function, [fmt.Printf](http://golang.org/pkg/fmt/).
## What Variables are Available in this Context?
You can use the template syntax, `$.`, to get the top-level template context from anywhere in your template. This will print out all the values under, `.Site`.
```
{{ printf "%#v" $.Site }}
```
This will print out the value of `.Permalink`:
```
{{ printf "%#v" .Permalink }}
```
This will print out a list of all the variables scoped to the current context
(`.`, aka ["the dot"][tempintro]).
```
{{ printf "%#v" . }}
```
When developing a [homepage][], what does one of the pages you're looping through look like?
```
{{ range .Data.Pages }}
{{/* The context, ".", is now each one of the pages as it goes through the loop */}}
{{ printf "%#v" . }}
{{ end }}
```
{{% note "`.Data.Pages` on the Homepage" %}}
`.Data.Pages` on the homepage is equivalent to `.Site.Pages`.
{{% /note %}}
## Why Am I Showing No Defined Variables?
Check that you are passing variables in the `partial` function:
```
{{ partial "header" }}
```
This example will render the header partial, but the header partial will not have access to any contextual variables. You need to pass variables explicitly. For example, note the addition of ["the dot"][tempintro].
```
{{ partial "header" . }}
```
The dot (`.`) is considered fundamental to understanding Hugo templating. For more information, see [Introduction to Hugo Templating][tempintro].
[homepage]: /templates/homepage/
[tempintro]: /templates/introduction/