{"body_class":"rp-body rp-section- rp-full-js","section":"project","thisUrl":"https://regisphilibert.com/tags/variables/","title":"Regis Philibert | Variables","translation_url":null}


Hugo .Scratch explained

Working variables in Hugo can be complicated when coming from classic languages.

What you usually do :

$greetings = "Good Morning";
if($sky == "dark"){
	$greetings = "Good Night";
echo $greetings;

That next bit of code would be tempting :

{{ $greetings := "Good Morning" }}
{{ if eq $sky "dark" }}
	{{ $greetings := "Good Night" }}
{{ end }}
{{ $greetings }}

But that won’t happen :(

The only way to override variables or attach any kind of value to a .Page object is to use .Scratch

.Scratch is a life saver but its documentation is a bit light if, like me, you are not comfortable with the Go language.

Here is my take about Hugo’s .Scratch and how it can help you.

Read more ...