hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

template-debugging.md (2106B)

    1 ---
    2 title: Template Debugging
    3 # linktitle: Template Debugging
    4 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.
    5 date: 2017-02-01
    6 publishdate: 2017-02-01
    7 lastmod: 2017-02-01
    8 categories: [templates]
    9 keywords: [debugging,troubleshooting]
   10 menu:
   11   docs:
   12     parent: "templates"
   13     weight: 180
   14 weight: 180
   15 sections_weight: 180
   16 draft: false
   17 aliases: []
   18 toc: false
   19 ---
   20 
   21 Here are some snippets you can add to your template to answer some common questions.
   22 
   23 These snippets use the `printf` function available in all Go templates.  This function is an alias to the Go function, [fmt.Printf](https://golang.org/pkg/fmt/).
   24 
   25 ## What Variables are Available in this Context?
   26 
   27 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`.
   28 
   29 ```
   30 {{ printf "%#v" $.Site }}
   31 ```
   32 
   33 This will print out the value of `.Permalink`:
   34 
   35 
   36 ```
   37 {{ printf "%#v" .Permalink }}
   38 ```
   39 
   40 
   41 This will print out a list of all the variables scoped to the current context
   42 (`.`, aka ["the dot"][tempintro]).
   43 
   44 
   45 ```
   46 {{ printf "%#v" . }}
   47 ```
   48 
   49 
   50 When developing a [homepage][], what does one of the pages you're looping through look like?
   51 
   52 ```
   53 {{ range .Pages }}
   54     {{/* The context, ".", is now each one of the pages as it goes through the loop */}}
   55     {{ printf "%#v" . }}
   56 {{ end }}
   57 ```
   58 
   59 ## Why Am I Showing No Defined Variables?
   60 
   61 Check that you are passing variables in the `partial` function:
   62 
   63 ```
   64 {{ partial "header.html" }}
   65 ```
   66 
   67 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].
   68 
   69 ```
   70 {{ partial "header.html" . }}
   71 ```
   72 
   73 The dot (`.`) is considered fundamental to understanding Hugo templating. For more information, see [Introduction to Hugo Templating][tempintro].
   74 
   75 [homepage]: /templates/homepage/
   76 [tempintro]: /templates/introduction/