eleventy
eleventy copied to clipboard
eleventyComputed value isn't available current page, but is in item data
Operating system
Windows 11
Eleventy
2.0.1
Describe the bug
If I set a variable in eleventyComputed
, then try to use that variable in the current page, it has no value. However, if I loop over a collection containing the page with the eleventyComputed
value it's properly set and accessible as item.data.variableName.
The page I've been using to test this uses pagination, but I'm not trying to use computed data in pagination as was done in several other reported issues. This happens even if I just use a simple string for the value in eleventyComputed
.
I created a GitHub repo demonstrating the problem: https://github.com/VAggrippino/eleventy-computed-title
The built output of a page with two variables set in eleventyComputed
, title
and nottitle
, has no value for either {{ title }}
or {{ nottitle }}
when they are used in the HTML of the same page.
However, in a previous attempt I was able to get a value for the second variable. This scenario has been reproduced by unaffiliated developers.
When I loop over the pages with these computed variables in a collection (e.g. {% for item in collections.months %}
), both item.data.title
and item.data.nottitle
are available.
The behavior suggests to me that there might be an asynchronous function somewhere that's not resolving in time for the current page.
Reproduction steps
- https://github.com/VAggrippino/eleventy-computed-title/blob/main/pagination-month.njk :
---
layout: base.njk
tags:
- posts
- months
pagination:
data: collections.contentByMonth
size: 1
alias: month
addAllPagesToCollections: true
permalink: "{{ month }}/index.html"
eleventyComputed:
title: "Posts from {{ month }}"
nottitle: "(Not Title) Posts from {{ month }}"
---
<h1>Title: {{ title }}</h1>
<h1>Not Title: {{ nottittle }}</h1>
{% for post in collections.contentByMonth[month] %}
<article>
{{ post.content | safe }}
</article>
{% endfor %}
-
npx @11ty/eleventy
- The page built from this code has no value for either
{{ title }}
or{{ nottitle }}
- The page built from this code has no value for either
-
https://github.com/VAggrippino/eleventy-computed-title/blob/main/index.njk :
---
layout: base.njk
---
<h2>Month Pages (title):</h2>
<ul>
{% for month in collections.months %}
<li><a href="{{ month.page.url }}">{{ month.data.title }}</a></li>
{% endfor %}
</ul>
<h2>Month Pages (not title):</h2>
<ul>
{% for month in collections.months %}
<li><a href="{{ month.page.url }}">{{ month.data.nottitle }}</a></li>
{% endfor %}
</ul>
This index page works fine. Both month.data.title
and month.data.nottitle
have the expected values.
Expected behavior
I expected the variables set in eleventyComputed
to be available in the current page.
I thought there might be something special about the title
variable, but I was later able to reproduce the problem with arbitrary variable names.
I thought there might be some issue with using pagination data in eleventyComputed
(not using computed data in pagination
), but I was able to reproduce the problem when setting the variable values to a simple string.
Reproduction URL
https://github.com/VAggrippino/eleventy-computed-title
Screenshots
Pictured in the screenshot is Vivaldi because it has a really nice tiled tabs feature, but all testing was done in Google Chrome.
Hi, this is specifically a problem with nunjucks. If you change your templates to liquid, md, handlebars, etc... it will work properly and eleventyComputed
values will be computed
more info -> #3120
#3120 was merged and shipping with 3.0.0-alpha.11. Thank you!