Unexpected results when backing a resources.Get call with an or?

so, I’ve seen the pattern of:

{{- $myscratch := newScratch -}}
{{- with $file := resources.Get (or .Params.image site.Params.image ) -}}
   {{- $myscratch.Set "resource" $file -}}
{{- end -}}
...

used in multiple places with success…

I’m noticing a surprising ( to me) behavior when using variables as values:

devhost-docker/blogsite (wpl_main)$ ls assets/icons/wolfspyre*
assets/icons/wolfspyre.svg
devhost-docker/blogsite (wpl_main)$ ls assets/icons/poo*
assets/icons/poo-storm-duotone.svg  assets/icons/poo-storm-solid.svg  assets/icons/poo.svg
{{- $iconPlus := newScratch -}}
{{- /* ... */ -}}
{{- $iconPlus.Set "iconResource" false -}}
{{- /* ... */ -}}
{{- with .Get 0 -}}{{- $iconPlus.Set "icon" . -}}{{- end -}}
{{- /* ... */ -}}
{{- $iconDuo  := (printf "icons/%s-duotone.svg"  ($iconPlus.Get "icon") ) -}}
{{- $iconSolo := (printf "icons/%s.svg"  ($iconPlus.Get "icon") ) -}}
{{- with $icon := resources.Get (or $iconDuo $iconSolo)  -}}
  {{- warnf "[DEBUG][iconPlus][%s][%s] found: %s  " ($iconPlus.Get "pagepath") ($iconPlus.Get "icon") $icon.Name -}}
  {{- $iconPlus.Set "iconResource" $icon -}}
{{- end -}}
{{- /* ... */ -}}
{{- if eq ($iconPlus.Get "iconResource") false -}}
  {{- $iconPlus.Set "err" (printf "[WARN][iconPlus][%s][%s] Resources.get for %s/%s.svg failed. Check the path is correct or remove the shortcode call." ($iconPlus.Get "pagepath") ($iconPlus.Get "icon") ($iconPlus.Get "icon") ) -}}
{{- end -}}

the or seems not to work as expected:

{{< iconplus "wolfspyre" >}} 
{{< iconplus "poo-storm" >}} 
WARN  [WARN][iconPlus][/snippits/wplshortcodes/shortcodes/iconplus][wolfspyre] Resources.get for icons/wolfspyre.svg failed. Check the path is correct or remove the shortcode call.
WARN  [DEBUG][iconPlus][/snippits/wplshortcodes/shortcodes/iconplus][poo-storm] found: /icons/poo-storm-duotone.svg

and when inverting the order:

{{- $iconPlus := newScratch -}}
{{- /* ... */ -}}
{{- with .Get 0 -}}{{- $iconPlus.Set "icon" . -}}{{- end -}}
{{- $iconDuo  := (printf "icons/%s-duotone.svg"  ($iconPlus.Get "icon") ) -}}
{{- $iconSolo := (printf "icons/%s.svg"  ($iconPlus.Get "icon") ) -}}
{{- with $icon := resources.Get (or $iconSolo $iconDuo)  -}}
  {{- warnf "[DEBUG][iconPlus][%s][%s] found: %s  " ($iconPlus.Get "pagepath") ($iconPlus.Get "icon") $icon.Name -}}
  {{- $iconPlus.Set "iconResource" $icon -}}
{{- end -}}
{{- /* ... */ -}}
WARN  [DEBUG][iconPlus][/snippits/wplshortcodes/shortcodes/iconplus][wolfspyre][namedParams] found: /icons/wolfspyre.svg
WARN  [WARN][iconPlus][/snippits/wplshortcodes/shortcodes/iconplus][poo-storm] Resources.get for icons/poo-storm.svg failed. Check the path is correct or remove the shortcode call.

I was originally doing this as a
{{- with foo := (or (printf "...%s.svg " (var) ) (printf "...%s-duotone.svg " (var) ) ) -}}
when I notriced the issue, and moved the variable definition outside the with just to make sure I wasn’t doing something obviously dense.

I was using 0.124.1, and just validated this still occurs on 0.125.2

So… my question is …

is this expected behavior?

If so… what am I missing here?

I’d expect the failing or to pass thru.

My current thought is to retool and use getMatch, but I’d like to understand the nuance occurring here…

Can you simplify your question and example?

HI JOE!
Always a pleasure. Hope your saturday is going fantastically.

Thank you for your help.

<3

here’s a minimal demo of the issue I’m seeing:
given two assets:

  • assets/icons/wolfspyre.svg
  • assets/icons/poo-storm-duotone.svg

should these shortcodes behave the same:

  • layouts/shortcodes/iconplusa.html
{{- $icon := .Get "icon" -}}

{{- $iconDuo  := (printf "icons/%s-duotone.svg" $icon) -}}
{{- $iconSolo := (printf "icons/%s.svg" $icon ) -}}
{{- with $resource := resources.Get (or $iconSolo $iconDuo )  -}}
  {{- $resource.Content |safeHTML -}}
  {{- warnf  "[DBUG][iconPlusA][%s] Found icons/%s.svg" $resource.Name $icon -}}
{{- else -}}
  {{- warnf  "[WARN][iconPlusA][%s] Resources.get for icons/%s.svg failed. Check the path is correct or remove the shortcode." $icon $icon -}}
{{- end -}}
  • layouts/shortcodes/iconplusb.html:
{{- $icon := .Get "icon" -}}

{{- $iconDuo  := (printf "icons/%s-duotone.svg" $icon) -}}
{{- $iconSolo := (printf "icons/%s.svg" $icon ) -}}
{{- with $resource := resources.Get (or $iconDuo $iconSolo )  -}}
  {{- $resource.Content |safeHTML -}}
  {{- warnf  "[DBUG][iconPlusB][%s] Found icons/%s.svg" $resource.Name $icon -}}
{{- else -}}
  {{- warnf  "[WARN][iconPlusB][%s] Resources.get for icons/%s.svg failed. Check the path is correct or remove the shortcode." $icon $icon -}}
{{- end -}}

with the following in a parsed file:

foo.md


## Testing initial order of vars fed to the (or )

{{< iconplusa icon="wolfspyre" >}}
{{< iconplusa icon="poo-storm" >}}

## Testing inverted order of vars fed to the (or )

{{< iconplusb icon="wolfspyre" >}}
{{< iconplusb icon="poo-storm" >}}

emits:

WARN  [DBUG][iconPlusA][/icons/wolfspyre.svg] Found icons/wolfspyre.svg
WARN  [WARN][iconPlusA][poo-storm] Resources.get for icons/poo-storm.svg failed. Check the path is correct or remove the shortcode.
WARN  [WARN][iconPlusB][wolfspyre] Resources.get for icons/wolfspyre.svg failed. Check the path is correct or remove the shortcode.
WARN  [DBUG][iconPlusB][/icons/poo-storm-duotone.svg] Found icons/poo-storm.svg

is that expected?

I was under the IMPRESSION that the or would fall thru and eval the second target, as happens with .Params.thing and site.Params.thing

but it looks like the falsey return of the eval of the file resource
(as opposed to the nil eval of the param)

kills the with evaluation entirely, rather than evaling the next or… (I’m guessing here)

resources.Get (or $iconSolo $iconDuo )

What are you trying to do?

if resources.Get fails see if this other resource exists and try that…
ie: if this or that exists …
return whichever does exist… if the first exists, return that.

Do this:

{{ with or (resources.Get "foo") (resources.Get "bar") }}

That should work, and makes sense…
(( Thank you for sharing your advise and knowledge here :wink: )

I was posting this here because I’m not sure I understand why it’s behaving as it does, and I’m trying to better grok the nuance.

Does the existing behavior of resources.get make sense to you?

Like I said, I was under the IMPRESSION that the
( or fileA fileB ) would fall thru and eval the second target, as happens with
( or .Params.thing site.Params.thing)

an so I’m trying to understand what I’m missing :wink:

I think if you simplify your example and test it you will figure it out.

MAAAAAYBE ? :slight_smile:
IDK…
You may have more faith in my intuition than I do
:wink:

but in all seriousness:

is it that running resources.get ( or fileA fileB ) only performs one evaluation as the or returns the first string val.

(both of which are ‘truthy’ from the perspective of or )
whereas a nil will fallthru?

That one

1 Like

thanks. I really just wanted to better understand the WHY before callin it. this stuff is… nuanced :wink:

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