Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: unable to cast <nil> since hugo v0.87 #8865

Closed
AndreasAZiegler opened this issue Aug 7, 2021 · 10 comments
Closed

Error: unable to cast <nil> since hugo v0.87 #8865

AndreasAZiegler opened this issue Aug 7, 2021 · 10 comments
Labels

Comments

@AndreasAZiegler
Copy link

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.87.0+extended linux/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes

Since hugo v0.87.0 we get the error

Error: Error building site: failed to render pages: render of "section" failed: "/data/repos/hugo-web-ch/layouts/events/list.html:51:44": execute of template failed: template: events/list.html:51:44: executing "main" at <time .Params.date_end "Local">: error calling time: unable to cast <nil> of type
<nil> to Time

for this part of the code

{{/* Render only events which: */}}
{{/* - end date is given (= not unset, "ne" "nil"), and this end date */}}
{{/*   is later than the limit; */}}
{{/* - OR end date is not given (= is unset, "eq" "nil"), and then */}}
{{/*   the start date is later than the limit. */}}
{{/* Other events, even if published, are considered as over, and */}}
{{/* will not be displayed in the list anymore. */}}
{{ if or
   (and (ne .Params.date_end nil) (ge (time .Params.date_end "Local") $limit_date))
   (and (eq .Params.date_end nil) (ge .Params.date $limit_date))
}}
@ghost
Copy link

ghost commented Aug 9, 2021

Not sure if this is a bug or not, but first you should just be able to do something like:

{{if or
	(and .Params.date_end (ge (time .Params.date_end "Local") $limit_date))
	(and (not .Params.date_end) (ge .Params.date $limit_date))
}}

Second, if you'd like to cleanup your code a bit you can include all your comments into a single comment:

{{/*	Render only events which:
	- end date is given (= not unset, "ne" "nil"), and this end date is later than the limit;
	- OR end date is not given (= is unset, "eq" "nil"), and then the start date is later than the limit.

	Other events, even if published, are considered as over, and will not be displayed in the list anymore.
*/}}

@AndreasAZiegler
Copy link
Author

AndreasAZiegler commented Aug 9, 2021

@braderhart Thanks for your input.

I adjusted the code to

      {{/* Define a datetime limit to be the present day at 0:00. */}}
      {{ $limit_date := time (substr (now.AddDate 0 0 0) 0 10) "Local" }}
      {{ range sort .Pages ".Params.date" "asc" }}

      {{/* Render only events which: */}}
           - end date is given, and this end date is later than the limit;
           - OR end date is not given, and then the start date is later than the limit.

           Other events, even if published, are considered as over, and will not be displayed in the list anymore.
      */}}
      {{ if or
        (and .Params.date_end (ge (time .Params.date_end "Local") $limit_date))
        (and (not .Params.date_end) (ge .Params.date $limit_date))
      }}

but I still get the error

Error: Error building site: failed to render pages: render of "section" failed: "/data/repos/hugo-web-ch/layouts/events/list.html:50:35": execute of template failed: template: events/list.html:50:35: executing "main" at <time .Params.date_end "Local">: error calling time: unable to cast <nil> of type
<nil> to Time

@bep
Copy link
Member

bep commented Aug 9, 2021

I'm not sure how the above could have changed in the latest release, but we should handle nil inputs, so I will fix that.

That said, the reason @braderhart 's suggestion does not work is that and/or in Go templates does not short circuit (this will be fixed in Go 1.17).

@bep bep closed this as completed in 3e11072 Aug 9, 2021
@bep
Copy link
Member

bep commented Aug 10, 2021

I have reverted the above "fix" after thinking about it:

  • nil is not a valid date/time
  • The reason your code now fails is that we fixed the error handling in time function, so you now get an error and not just a string describing the error.

@AndreasAZiegler
Copy link
Author

Then how can I adjust the code to avoid nil?

@jmooring
Copy link
Member

@AndreasAZiegler Please show an example of event frontmatter.

@AndreasAZiegler
Copy link
Author

Below an example of date and date_end:

date: 2021-08-25T18:00:47+0200
date_end: 2021-08-25T21:00:00+0200

@jmooring
Copy link
Member

@AndreasAZiegler

There may be a more concise way to do this...

{{ range .Pages }}

  {{ $render := false }}

  {{ with .Params.date_end }}
    {{ if (time .).After $limit_date }}
      {{ $render = true }}
    {{ end }}
  {{ else }}
    {{ if .Date.After $limit_date }}
      {{ $render = true }}
    {{ end }}
  {{ end }}

  {{ if $render }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}

{{ end }}

@jmooring
Copy link
Member

More concise...

{{ range $p := .Pages }}
  {{ with or .Params.date_end .Date }}
    {{ if (time .).After $limit_date }}
      <h2><a href="{{ $p.RelPermalink }}">{{ $p.Title }}</a></h2>
    {{ end }}
  {{ end }}
{{ end }}

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants