Débuguer les Modèles
Voici quelques fragments que vous pouvez ajouter à votre modèle pour répondre à quelques questions communes.
Ces fragments utilisent la fonction printf disponible dans tous les modèles Go. Cette fonction est un alias de la fonction Go, fmt.Printf.
Quelles Variables sont Disponibles dans ce Contexte ?
Vous pouvez utiliser la syntaxe de modèle, $., pour récupérer le contexte top-level du modèle à partir de n’importe où dans votre modèle. Ceci imprimera toutes les valeurs sous .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”).
{{ 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 }}
Pourquoi je ne Vois Pas de Variables Définies ?
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”.
{{ partial "header" . }}
The dot (.) is considered fundamental to understanding Hugo templating. For more information, see Introduction to Hugo Templating.