fileExists doesn't check for theme files?

Hey, I wanted to check if a partial existed and if not replace it with a placeholder, so I ended up doing this:

{{ $areaTemplate := printf "base/%s.html" $a }}
{{ if fileExists (printf "themes/vaytheme/layouts/partials/%s" $areaTemplate) }}
    {{ partial $areaTemplate $ }}
{{ else }}
    {{/* Add area placeholder or leave empty, depending on site config */}}
    {{ if (default true $.Site.Params.vaytheme.enablePlaceholders) }}
        <div style="grid-area: {{ $a }}">
            {{ $areaTemplate }}
    {{ end }}
{{ end }}

I realized the fileExists function operates from the site root so in this example I added “themes/vaytheme/” to the path to be able to find my templates. However, I would like to have my theme templates able to be overridable by templates in the site root.

I think it is overkill to have two ifs to accomplish what’s considered a core Hugo feature to overlay site root files over theme root files. A way to accomplish this without hardcoding the theme path would be to have a Site.Theme variable as proposed here. However the idea was discarded.

So a solution I see would be to add a boolean parameter to the fileExists function with a default of false. This parameter would allow to additionally check for the given filename from the theme root if no file was found in the site root. Defaulting to false, it would not break previous behaviour.

It would be usable like this:

{{ if fileExists (printf "layouts/partials/%s" $areaTemplate) true }}
  {{/* file exists in either site root or theme root (with priority for site root) */}}
{{ else }}
  {{/* file does not exist */}}
{{ end }}

What do you think about this?

1 Like

I don’t understand the problem here. Are two ifs an ‘overkill problem’? I don’t think so. At least I don’t hope so, because otherwise I have a lot of problems in my code. :slight_smile:

I also don’t see why not having a theme variable is a problem. If my theme file is in themes/my-theme/layouts/partials/header.html, then I can safely hard-code my-theme as the folder name in header.html. That’s because header.html shouldn’t be somewhere else (it’s dependent on and interrelated with the other theme files).

So I think I misunderstood you. (I’m not saying there’s no problem at all here.) :slight_smile:

But I don’t think changing fileExists works here. Also because it looks like your proposal changes the meaning of false. Currently, when fileExists returns false we know that there is no file at the given path. If your proposal checks two paths, we don’t know if false means that path A doesn’t exist or path B doesn’t exist.

1 Like

Hi, and thank you for taking the time to reply.

Well the default behaviour would not be altered. Only if you pass it “true” as a second argument (to ask to check into theme files), then it would return true if any was found, or false if none of the two was found. Do we agree this is a desirable behaviour that wouldn’t break existing code?

Well, sorry I got off-topic about the theme variable. That’s a totally different topic. But my personal view on the matter is you should be able to copy my theme in a different folder (rename it), modify a few bits here and there, and get it running without having to edit all the paths to assets and such.

Am I more clear now? :smiley:

1 Like