Another weird issue with #Eleventy #Nunjucks templates…

I have these two lines:

{% set photos = page.data.photos %}
{{ page.data.photos | length }} / {{ photos | length }}

The result is:

0 / 24

What the… 🤨

EDIT: the issue was me all along… 😞

https://mamot.fr/@nhoizey/115028557519497415

Nicolas Hoizey (@[email protected])

@[email protected] ooooooh, I feel so dumb! In the layout for the page, `photos` is directly available, it's not in `page.data.photos`! 🤪 My assignment was not required, and actually did nothing. I don't know why I thought data coming from eleventyComputed in a directory data file was always in page.data.… 🤷‍♂️

Mamot - Le Mastodon de La Quadrature du Net
@nhoizey Weird. 😅 What's in your console if you add `| log` before each `| length`? 2× 24 items or 1× 0 and 1× 24?
@chriskirknielsen the later… 🤷‍♂️

@nhoizey What happens when you use the same value for both sides of the `/`? And what happens when you remove the `set` and only use the data object?

(I have no clue what's going on BTW, just seems like an interesting issue to debug 😅)

@chriskirknielsen

{% set photos = page.data.photos %}
{{ photos | log }}
{{ page.data.photos | log }}

-> array of photos and `undefined`

{{ page.data.photos | log }}
{% set photos = page.data.photos %}
{{ photos | log }}

-> `undefined` and array of photos

{{ page.data.photos | log }}
{% set photos = [] %}
{{ photos | log }}

-> `undefined` and empty array

The reason might be that the directory data file function that fills `page.data.photos` is asynchronous: https://github.com/nhoizey/activites.nicolas-hoizey.com/commit/02f01362421ad7f3fffa30a22e164e557906d8fa#diff-91e10fa2de323d22d405eaaa910c6d62f526eb7aebeda97514342ca532052ffaR12-R106

Déplacement de la logique de calcul de la trace et des photos dans un… · nhoizey/activites.nicolas-hoizey.com@02f0136

… directory data file

GitHub
@nhoizey Ah, async could definitely be the culprit, though I'm not sure. Could you try `{% setAsync 'photos' = age.data.photos %}`? (not even sure you can do an assignment of an async variable — I only know of it for capturing content between setAsync/endsetAsync) Maybe `{% setAsync 'photosAsync' %}{{ age.data.photos | dump }}{% endsetAsync %}{% set photos = photosAsync | JSONParse %}` (this would require you to create a JSONParse filter in Eleventy, I don't think Nunjucks has that built in)

@chriskirknielsen ooooooh, I feel so dumb! In the layout for the page, `photos` is directly available, it's not in `page.data.photos`! 🤪

My assignment was not required, and actually did nothing.

I don't know why I thought data coming from eleventyComputed in a directory data file was always in page.data.… 🤷‍♂️

@nhoizey Oh yep that'll do it! 😄 I've definitely done that before, too. Now I'm just curious why your `set` didn't override the page data!

@chriskirknielsen I don’t know either! 🤷‍♂️

I still struggle after so many years with how to access each type of data, depending on where I want to get it, in a layout or in a loop… 😅