Is there any way for a theme to retrieve the `themeDir` value?


While trying to make the hugo search setup modular, and also allowing cache busting on one of the js files in there, I did this and it worked…

<!-- Below works *only if* this theme component is named "hugo-search-fuse-js".  -->
{{ $search_js_md5 := (readFile "themes/hugo-search-fuse-js/static/js/search.js" | md5) }}
<script src={{ printf "%s?%s" ("js/search.js" | absURL) $search_js_md5 }}></script>

But… in addition to the limitation that the component theme dir be named the same (hugo-search-fuse-js) by the user, the themesDir is hardcoded too (“themes/”).

Above fails for the exampleSite of the parent theme where I have themesDir set to "../../".

Building sites … ERROR 2018/06/11 12:11:40 Error while rendering “page” in “”: template: _default/search.html:35:22: executing “footer” at <readFile "themes/hug…>: error calling readFile: stat /home/kmodi/stow/pub_dotfiles/emacs/dot-emacs.d/elisp/ox-hugo/test/site/themes/hugo-bare-min-theme/exampleSite/content/themes/hugo-search-fuse-js/static/js/search.js: no such file or directory

I see that helpers/path.go has a GetThemesDir. Can you make that getter available in templates too? Then I don’t need to hard-code the themesDir.


We could, but I’m hestitant doing so … Adding these internals to the external API has a tendency to be “kick my leg” moments later.

And needing them is often a sign of “we should probably look for a better solution” for this particular problem.

As it is now, readFile checks:

  1. The working dir (as in your exampe starting with themes/)
  2. The content filesystem (which can be a composite)

We could consider adding the static filesystem (which would include the themes) to that lookup… You can create an issue about that. But…

I’m currently working on a resources.Resource template func (I will get back to you on the naming). This will be in Hugo 0.43 which will be a short release with some very cool stuff (we are talking next two weeks short).

With that you should be able to do:

{{ $searchJS := resources.Resource "js/search.js" | resources.Minify | resources.Fingerprint }}
<script src="{{ $searchJS.Permalink }}"></script>

There are possible options to the above (I guess the fingerprinting will default to sha256), but you get the point. This will handle any path issues as it will respect the theme overlays etc.

1 Like

Understood :slight_smile:

Will do… if that support is added… what will that readFile argument look like? Will then there be no need to have the themes/ portion in there?

Yes, that can easily get confused with the Page Bundle resources, but yes, what you suggest further would work perfectly for my use case!


You would drop the themes/ etc prefix.

1 Like