Get the actual `theme` variable

Hello there, @Lednerb here.

I’ve developed the Bilberry Hugo Theme and yesterday a user wanted to change the directory name from bilberry-hugo-theme to bilberry, but this caused an error on his side.

The problem is that I’ve wanted to code via the DRY principle and hacked on the partials includes:

{{  if or (fileExists (print "layouts/partials/content-type/" .Type ".html") ) (fileExists (print "themes/bilberry-hugo-theme/layouts/partials/content-type/" .Type ".html")) }}

Therefore I hardcoded the theme’s directory name.

Now I want to improve this so that the users can change the directory name.
I thought I can simply do something like this:

{{  if or (fileExists (print "layouts/partials/content-type/" .Type ".html") ) (fileExists (print "themes/" .Site.Theme "/layouts/partials/content-type/" .Type ".html")) }}

(Using a .Site.Theme variable or something similar because the theme var matches the themes directory name…)

But this variable is not available, and I’ve found nothing in the docs.

Do you have any idea to archive this?

A second issue will occur if the user changes the default themes/ folder.

I’m tracking this via: https://github.com/Lednerb/bilberry-hugo-theme/issues/26

Best regards and thanks for your help.

2 Likes

@bep, @digitalcraftsman

Do you guys have an approach to archive this?
Otherwise, it would be nice to have this kind of site variable in an upcoming HUGO release. (Feature Request)

I don’t think we will add a theme variable. You are the first asking for it, and to me it looks like a workaround/hack/symptom of another problem. Which means that we should try to solve “that other problem”.

Thx for your reply!

The only alternative that comes to my mind would be to create folders for each post type in the layouts/ directory and use this way to generate the layouts for the post types in my theme.

It seems that the restriction for the hardcoded theme name is not that important to copy the same default code for each post type.

The advantage of my workaround/hack is that users can quickly create their custom post types, without in-depth knowledge of the Hugo templating workflow or even HTML, by only editing one file.