Hugo: Global resources
All notes in this series:
- Automating hugo development with npm scripts
- normalize-scss with hugo
- Automatic image thumbnails in Hugo from static directory
- Escaping Hugo shortcodes within Hugo markdown
- Hugo tag and category pages
- Bind hugo to localhost for development
- Hugo 0.37 does not support h6 markdown heading
- Install Hugo testing distribution on Debian
- Hugo anchors next to headers
- Hugo: Migrating from Pygments to Chroma
- Hugo: Global resources
- Hugo: How to create a post series
In February 2018, I used a bit of a hack to create a global resources object by using Hugo v0.32 page resources, i.e. .Resources.Get.
In July 2018, Hugo v0.43 was released, which added resources.Get. This uses the global resources object rather than a page-specific .Resources.
Using resources.Get instead of .Resources.Get makes it trivial to use global resources. As the docs say:
This function [
resources.Get] operates on global resources. A global resource is a file within the assets directory, or within any directory mounted to the assets directory.
These notes how to switch from my hacky solution to the built-in global resources.
Switching from fakestatic to global resources §
Move assets from
content/fakestatic/fakepost/intoassets/, which is where global resource are stored.git mv content/fakestatic/fakepost/images assets/Delete
content/psuedostatic/fakepost/index.md, since it served only as a means of giving us an identifier for our fake global resources:git rm content/fakestatic/fakepost/index.mdDelete
script/fakestatic.js, since we no longer need to move any resources into thepublic/folder.git rm scripts/fakestatic.jsUpdate
package.jsonto remove call to thefakestatic.jsscript, since it was deleted.Update
img-general.htmlto remove the reference tofakestatic, and to useresources.GetMatchinstead, i.e.:- {{ $fakestatic := $context.Site.GetPage "page" "fakestatic/fakepost/index.md" }} - {{ $image := $fakestatic.Resources.GetMatch $filename }} + {{ $image := resources.GetMatch $filename }}
Conclusion §
The hack worked well, but using resources.Get for global resources is much simpler and more maintainable. Thanks to the Hugo developers for adding the functionality!