Different results for "if in .Params.tags" test

I’ve read every half-relevant previous forum question (and some Stack Overflow answers) about this issue, and have yet to find an answer that works for me; so I will throw myself on your collective mercies here.

The scenario: I am trying to thin down my overall SCSS/CSS by applying certain styling only on pages with one or more applicable tags — hence, I test for the presence in .Params.tags of the tag(s). Here’s a relevant section from the head.html partial (a repo branch link is at the bottom of this submission):

    {{- $cssBuild = slice (resources.Get "scss/critical.scss") -}}
    {{- if in .Params.tags "contact" -}}
      {{- $cssBuild = $cssBuild | append (resources.Get "scss/_billboard.scss") -}}
    {{- end -}}
    {{- $css := $cssBuild | resources.Concat "scss/css.scss" | resources.ToCSS $optionsCSSFileComp | fingerprint "md5" -}}

The problem: the if in .Params.tags "contact" line doesn’t return as true even if there’s a tags: contact entry on the page, so the append doesn’t happen, thus leaving the desired tag-specific SCSS out of the final result. Yet, weirdly, in a test instance further down on the page (in a content: item in a noscript-related style), the presence of “contact” in .Params.tags does test as true, as it should.

Also, if I take the append out of the conditional, the append works fine, although that’s not what I want since I’m trying to pare down to only that SCSS which a page needs according to how I’ve tagged its front matter.

I based this in part on Issue when trying to only load page-specific SCSS stylesheets, and it apparently worked for him (albeit with some other, unrelated problems he reported). So there’s probably something I’m stupidly overlooking but, after staring at it off-and-on for the last 18 hours or so, I concede defeat. I’ve put this in a branch of my site repo at GitHub - brycewray/hugo_site at if-tags. Thanks in advance for any help someone may be able to offer!

I am unable to reproduce this behavior using the if-tags branch of your project.

1 Like

I appreciate your checking! I will give it another try tomorrow.

Following up from a few days later . . .

Since I could never get past the error I reported before, I ended up trying something else, which worked, but in a pretty ugly fashion (hugo_site/css.html at main · brycewray/hugo_site · GitHub) — so I’m now trying to pare it down to considerably DRY-er form and, agggh, am running into a similar problem.

Variables returned from a range show up as they should in tests (e.g., links with bogus attributes showing the variables), but I can’t get resources.ToCSS to accept the file if I supply its path via a variable rather than providing it as a string.

This works:

    {{ with $cssBuild -}}
      {{- $scss = resources.Get "scss/sectionals/home.scss" -}}
      {{- $css = $scss | resources.ToCSS $optionsCSS | fingerprint "md5" -}}
      <link rel="preload" as="style" href="{{ $css.RelPermalink }}">
      <link rel="stylesheet" href="{{ $css.RelPermalink }}" type="text/css">
    {{- end }}

But this doesn’t, even though the value of $cssBuild is scss/sectionals/home.scss:

    {{ with $cssBuild -}}
      {{- $scss = resources.Get $cssBuild }}
      {{- $css = $scss | resources.ToCSS $optionsCSS | fingerprint "md5" -}}
      <link rel="preload" as="style" href="{{ $css.RelPermalink }}">
      <link rel="stylesheet" href="{{ $css.RelPermalink }}" type="text/css">
    {{- end }}

With the latter, Hugo errors out with this message:

partials/css.html:81:34: executing "partials/css.html" at <resources.ToCSS>: error calling ToCSS: no Resource provided in transformation

I began to doubt my sanity at this point — especially after @jmooring couldn’t reproduce my earlier problem — so I even reverted to a couple of earlier Hugo versions (0.99.2, then 100.0) from 100.2, yet the result was the same in each case.

(Also: if it matters, I’m using macOS 12.4.)

I’ve put all this in a different repo branch, with the relevant partial at hugo_site/css.html at if-tags-3 · brycewray/hugo_site · GitHub. As before, I suspect this is something really stupid I’ve overlooked, but I just can’t see it after too many hours staring at it and seeing the inconsistent behavior over and over again. Thanks in advance to anyone who can show me what I’m doing wrong!

Do you have:

tags: content

or

tags:
  - content

If the former, this issue may be that you have a string instead of list context, so the in doesn’t work.

I think you’re referring to my original question rather than my last one; but, no, didn’t have any items with that tag.

Sorry, meant contact, not content… was thinking of the content directory while typing.

Yes, that’s on the “contact” page. However, I’m not testing for in now in the repo branch I linked most recently. (Don’t know if that affects your suggestion.)

Indeed I was answering your first issue; for your second issue, I’m looking it at it. I did notice that if I add:

{{ $curPage := .Page }}

at the top of the file, and around line the line 81 do the following:

    {{ with $cssBuild -}}
      {{- $scss = resources.Get $cssBuild }}
      {{- if $scss -}}
          {{/*  {{- $scss = resources.Get "scss/sectionals/home.scss" -}}  */}}
          {{- $css = $scss | resources.ToCSS $optionsCSS | fingerprint "md5" -}}
      <link rel="preload" as="style" href="{{ $css.RelPermalink }}">
      <link rel="stylesheet" href="{{ $css.RelPermalink }}" type="text/css">
      {{- else -}}
         {{- warnf (printf "No scss found for page %s" $curPage.TranslationKey) -}}
      {{- end -}}
    {{- end }}

That there are WARN on various pages, which indicates the resource is not actually being found.

I also see that cssBuild is not what you think it is: I see post-code-soc and post-img-soc and post-soc

1 Like

Interesting. I thought that the fallback would account for that possibility:

    {{ if eq $cssBuild "scss/sectionals/" -}}{{/* fallback, such as for /404.html */}}
      {{- $cssBuild = print $cssBuild "critical" -}}
      {{- $targetFilePrefix = "critical" -}}
    {{- end -}}

…since “scss/sectionals/” is the initial value for $cssBuild before the with/range stuff — but apparently not, from what you’re saying. I’ll give that a look. Thanks.

Yes, sir, you got it. I see now. All those slices with, e.g., post-code-soc should have been post-code-social (corresponding to the actual SCSS file name). (Whew.) Thank you very much!

1 Like

Resulting article (and again, thanks to both @jmooring and @cshoredaniel for the help:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.