Hugo

Use of readDir and readFile

In Hugo Themes Dont’s, it’s said that

  • readDir or readFile are not really meant for themes that are supposed to be usable under various hosts and various situations.

Lacking experience in web hosting, I’ve having difficulties in understanding this. Maybe it’s due to file I/O errors caused by Linux’s file permissions?

To understand this, let’s use the Minimo theme as an example. The link shows two places where readFile is used. One of them is to read parameters from a root-level YML config file named staticman.yml.

# layouts/partials/comments/staticman/GetStaticmanYML.html
{{ $rawStaticmanYML := ( readFile "staticman.yml" ) }}
{{ $parsedStaticmanYML := ( transform.Unmarshal $rawStaticmanYML ) }}

{{ return $parsedStaticmanYML }}

# layouts/partials/comments/staticman/form.html
{{- $staticman := ( partialCached "comments/staticman/GetStaticmanYML" . ) -}}
{{- $reCaptcha := $staticman.comments.reCaptcha | default ( dict "enabled" false ) -}}
...
    {{- if $reCaptcha.enabled -}}
      <input type='hidden' name='options[reCaptcha][siteKey]' value='{{ $reCaptcha.siteKey }}'>
      <input type='hidden' name='options[reCaptcha][secret]' value='{{ $reCaptcha.secret }}'>
    {{- end -}}

Can @onedrawingperday’s advice above be applied to this theme?

P.S. In this theme, Hugo uses the paramters in the YML config file for reCAPTCHA v2.

The Minimo theme does not have comments enabled in its Example Site.

The code blocks you’re quoting depend on whether line 85 of exampleSite/config.toml is enabled.

In my linked post the context of the quote refers to missing assets on the Hugo Themes showcase and sites that reside in subdirectories. readDir and readFile create hardcoded PATHs that are typically inaccessible in these cases.

Also note that in the themes repo README we mention that third party cookies for third party services should not be enabled in example sites.

Therefore what the Minimo theme’s author has done is fine.

And BTW the theme submission guidelines will be updated sometime next week. So you may want to keep an eye on this issue:

2 Likes