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

eleventyComputed value isn't available current page, but is in item data #3013

Closed
VAggrippino opened this issue Aug 4, 2023 · 2 comments
Closed
Labels

Comments

@VAggrippino
Copy link

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

  1. 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 %}
  1. npx @11ty/eleventy

    • The page built from this code has no value for either {{ title }} or {{ nottitle }}
  2. 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

image

Pictured in the screenshot is Vivaldi because it has a really nice tiled tabs feature, but all testing was done in Google Chrome.

@alifeee
Copy link
Contributor

alifeee commented Dec 10, 2023

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

@zachleat
Copy link
Member

#3120 was merged and shipping with 3.0.0-alpha.11. Thank you!

@zachleat zachleat added this to the Eleventy 3.0.0 milestone Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants