Hugo

[SOLVED] .File.UniqueID on zero object in Where clause

I am updating my theme because of this deprecation warning:
WARN Page's .UniqueID is deprecated and will be removed in a future release. Use .File.UniqueID.

Therefore I replaced all the .Page.UniqueID with .File.UniqueID and that works except for a where clause that lists the last 3 posts except the one that is currently shown:

{{ $pages := .Site.Pages }}
{{ with .File }}
          {{ $files := where $pages ".File" "!=" nil }}
          {{ $recent := where $files ".File.UniqueID" "!=" .UniqueID }}
          {{ $.Scratch.Set "recentPosts" $recent }}
{{ end }}

For the where it now shows:
WARN 2019/07/09 12:43:23 .File.UniqueID on zero object. Wrap it in if or with: {{ with .File }}{{ .UniqueID }}{{ end }}

If I understand it correctly, the first where should filter out all zero objects, right?

Thanks,
Max

1 Like

There are multiple issues here.

  1. If you use with .File then everything INSIDE of the loop is .File based. Meaning there is no .File.UniqueID because it is .UniqueID, with the dot being the .File reference from the with.
  2. This line {{ $recent := where $files ".File.UniqueID" "!=" .UniqueID }} probably did check if the currently ranged post is already shown on the currently shown page. And that’s not possible this way, because the .Page's .UniqueID is obsoleted.

If you can give more detail on what it was supposed to do we can dive deeper in how to achieve it. For now, the part that throws the error is ".File.UniqueID" because that at this location is .File.File.UniqueID.

On a second look, just remove the .File from the .UniqueID, because that one is relative to he files in $files I believe. That should not display the warning then.

Thanks for your input @pkollitsch

Making it relative makes it worse because then it falls back to the page collection and gives this deprecation warning:
WARN 2019/07/09 14:28:15 Page's .UniqueID is deprecated and will be removed in a future release. Use .File.UniqueID.

Context: I use the recent post collection in the sidebar of my blog (https://melcher.dev) to show the recent 3 posts. If one of the recent three posts is shown right now, I exclude that from the recent posts to not show them twice…

Let me try on my own blog and get back to you. The way I see it you can’t use UniqueID anymore to make this determination. Using .Title might do the trick - it might not come up too often that your posts identical titles.

Instead of this

{{ $files := where $pages ".File" "!=" nil }}
{{ $recent := where $files ".File.UniqueID" "!=" .UniqueID }}

Try this

{{ $recent := where site.RegularPages ".File.UniqueID" "!=" .UniqueID }}

Since regular pages will always have a non nil File object


Looking at this again, you don’t need to compare on UniqueID. You can compare page objects themselves (untested)

{{ $recent := where site.RegularPages  "." "!=" . }}

Also, the complement func would work here https://gohugo.io/functions/complement/#readout

It’s weird, I am doing this on my site, but it includes the current post in the list:

{{ $recentPages := where site.RegularPages ".File.UniqueID" "!=" .UniqueID }}
{{ range first 5 $recentPages }}
    {{ .Title }}
{{ end }}

then using this one does not show anything:

{{ $recentPages := where site.RegularPages  "." "!=" . }}
{{ range first 5 $recentPages }}
    {{ .Title }}
{{ end }}

I am not sure if that is an issue in my own blog or general.

Ok, now fixed and tested, this works:

{{ $recentPages := where site.RegularPages ".File.UniqueID" "!=" .UniqueID }}
<ul>
{{ range first 5 $recentPages }}
    <li>{{ .Title }}
{{ end }}
</ul>

@zwbetz your solution with the dots for some reason returns an empty list.

It works - but it will be deprecated. The last .UniqueID is referencing the .Page context, right?

Does not work for me.

Again, the last .UniqueID is referencing the .Page context - and that gives me:
Page's .UniqueID is deprecated and will be removed in a future release. Use .File.UniqueID.

I totally forgot that: I had the issue before and the way to use file within a page object is .Page.File.Variable. So this works without warning (already finished up to fit into my site):

<aside class="card mb-3" id="widget_archive">
    <h3 class="card-header">
        Neueste Eintr&auml;ge
    </h3>
    {{ $recentPages := where site.RegularPages ".File.UniqueID" "!=" .File.UniqueID }}
    <ul class="list-group list-group-flush">
        {{ range first 5 $recentPages }}
            <li class="list-group-item">
                <a href="{{ .Permalink }}">
                    {{ .Title }}
                </a>
            </li>
        {{ end }}
    </ul>  
</aside>

Thanks yall for testing that. In hindsight it makes sense why it would return no pages, since the comparison is comparing the page to itself. So it’d need to updated to compare against an individual page, so doing a site.GetPage "foo" beforehand

Not 100% working for me - I get the

WARN 2019/07/10 09:47:47 .File.UniqueID on zero object. Wrap it in if or with: {{ with .File }}{{ 
.UniqueID }}{{ end }}

If I use that. Its the only occurrence of .UniqueID in my theme so its coming from this.

Any other ideas @pkollitsch @zwbetz?

Thanks,
Max

Live with the warning for some days (it’s a warning, not an error ;). I tried to test my idea from earlier to use .Title instead of .UniqueID and that does not work. I will find a way - having the same problem. I will be out of IT-equipment for a day or two, so bear with me.

<!-- though using 'where' here is probably redundant;
    $pages := .Site.RegularPages    should be fine
 -->
{{ $pages := where .Site.RegularPages ".File.UniqueID" "!=" "nil" }}

{{ with .File }}
  {{ $recent := where $pages ".File.UniqueID" "!=" .UniqueID }}
  {{ $.Scratch.Set "recentPosts" $recent }}
{{ end }}

If you only wanted to exclude the current page from the selection though:

{{ $currentPage := . }}
{{ $recent := $pages | complement (slice $currentPage) }}
3 Likes

Genius! Thanks for that @pointyfar!

Just for the protocoll, my “recent posts” widget looks like this now:

<aside class="card mb-3" id="widget_recent">
    <h3 class="card-header">
        Neueste Eintr&auml;ge
    </h3>
    {{ $pages := where .Site.RegularPages ".File.UniqueID" "!=" "nil" }}
    {{ $currentPage := . }}
    {{ $recentPages := $pages | complement (slice $currentPage) }}
    <ul class="list-group list-group-flush">
        {{ range first 5 $recentPages }}
            <li class="list-group-item">
                <a href="{{ .Permalink }}">
                    {{ .Title }}
                </a>
            </li>
        {{ end }}
    </ul>  
</aside>
1 Like