Question about hugo syntax and scratch

This theme has several instances of .Scratch and I have not seen this in any other themes so far. Is this like an arbitrary decision by the theme author or can anyone more knowledgeable kindly explain what the scratches are doing?

I guess you have read the docs all around Scratch | Hugo

It’s a way to add new values to a page within template code. These can be retrieved later. Even outside the template that stores the value.

A template variable cannot be accessed from outside the current tamplate.

Looks like the theme auther wants to use values calculated in basof.html in other called partials later.

That way the values have to be calculated once.

  • calculate value
  • store that value scratch.Set
  • Call a partial passing the current page as context
    • retrieve the value scratch.Get

IMHO the more common way for the shown case would be partials.IncludeCached | Hugo

Scratches have some pitfalls also described in the docs.


Thats a quite general answer without digging too deep in the template code around…

I did but couldn’t grasp it for a whiile.

I see. Now I grasp it.

This is an interesting concept in Hugo. But running the template metrics suggests some files to be cached that shouldn’t be, e.g page navigation (prev, next).

The metrics are only a hint. Imho there’s no code analysis behind. Maybe the template context…but out of my knowledge.

The decision to cache depends on you. A navigation may have static parts shown on all pages…

Thats why I wrote “in the schown use case”. Thats calculating things based on site.params. which won’t change per page.

Same for scratch. In that use case it should work. But switching pages with scratch is not that intuitive.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.