Sam Hooke

Hugo: Global resources

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 §

  1. Move assets from content/fakestatic/fakepost/ into assets/, which is where global resource are stored.

    git mv content/fakestatic/fakepost/images assets/
  2. Delete content/psuedostatic/fakepost/, since it served only as a means of giving us an identifier for our fake global resources:

    git rm content/fakestatic/fakepost/
  3. Delete script/fakestatic.js, since we no longer need to move any resources into the public/ folder.

    git rm scripts/fakestatic.js
  4. Update package.json to remove call to the fakestatic.js script, since it was deleted.

  5. Update img-general.html to remove the reference to fakestatic, and to use resources.GetMatch instead, i.e.:

    - {{ $fakestatic := $context.Site.GetPage "page" "fakestatic/fakepost/" }}
    - {{ $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!