eleventy
eleventy copied to clipboard
eleventyComputed is not returning data
---
pagination:
data: standards
size: 1
alias: standard
addAllPagesToCollections: true
permalink: "{{ standard.slug }}/index.html"
eleventyComputed:
title: "{{ standard.name }}"
---
Doesn't return anything for the <title>.
Prior, I was using {% set title = standard.name %}, but this doesn't get elevated to collections.all (returns empty for dynamic pages)
Not sure, what the issue is without seeing more of your example code.
Here's a minimal and totally functional project creating pages from _data files. Titles work.
// .eleventy.js
module.exports = function (config) {
// Layouts
config.addLayoutAlias(`base`, `base.njk`);
// Base Config
return {
dir: {
input: `src`,
output: `dist`,
includes: `_includes`,
layouts: `_layouts`,
data: `_data`,
},
templateFormats: [`njk`, `md`],
htmlTemplateEngine: `njk`,
markdownTemplateEngine: `njk`,
};
};
{# src/dog.njk #}
---
layout: base
pagination:
data: dogs
size: 1
alias: dog
addAllPagesToCollections: true
permalink: "/dogs/{{ dog.name | slug }}/index.html"
eleventyComputed:
title: "{{ dog.name }}"
---
<div>
<div>Name: {{dog.name}}</div>
<div>Age: {{dog.age}}</div>
<div>Date: {{dog.date}}</div>
</div>
{# src/_layouts/base.njk #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ title }}</title>
</head>
<body>
<div>
{{ content | safe }}
</div>
</body>
</html>
// src/_data/dogs.json
[
{
"name": "Fluffy",
"age": 2,
"date": "2020-06-23T00:00:00.000Z"
},
{
"name": "Pennywise",
"age": 9,
"date": "2020-04-23T00:00:00.000Z"
}
]
I have the same issue here as well.
---
eleventyComputed:
title: "{{ i18n[lang]['index.njk'].pageTitle }}"
excerpt: "{{ article.meta}}"
author: "{{ article.user.username}}"
image: "{{ article.featureImage}}"
lang: "{{ homepage.lang }}"
eleventyNavigation:
key: "home-{{ homepage.lang }}"
order: 1
title: "{{ i18n[lang]['index.njk'].pageTitle }}"
permalink: "/{{ homepage.lang | urlEncode}}/"
---
When I do {{ title | log }}, it returns nothing.
@jasonday Is this still a problem? If so, do you have an example repo I can look at?
@b3u Unfortunately, I don't have a repo up to illustrate the issue, but I get empty title for all dynamic pages, although I'm following the same setup:
---
pagination:
data: htmlelements
size: 1
alias: htmlelement
addAllPagesToCollections: true
permalink: "{{ htmlelement.slug }}/index.html"
eleventyComputed:
title: "{{ htmlelement.tag }}"
---
{% extends 'page.njk' %}
in page.njk:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{ title }}</title>
@jasonday to access computed variables, use:
{{ eleventyComputed.title }}
If you're also sometimes using {{ title }}, then do this:
{{ title or eleventyComputed.title }}
@edwardhorsford I haven't needed to use computed variables that way. For me, {{ title }} works.
@jasonday There's not much I can do if I can't replicate the issue via a test repo. If you could create one, that would help a lot.
I think it has to do with how I reference it in my collections.all loop, but I've tried all of the permutations.
I'm creating a json object for a search index:
---
permalink: "/api/searchData-backup.json"
eleventyExcludeFromCollections: true
---
{
"searchData": [
{%- for item in collections.all -%}
{%- if not item.data.excludeFromSitemap %}
{
"title": "{{ item.data.title or item.data.eleventyComputed.title }}",
"url": "{{ item.url }}",
"date": "{{ item.date }}",
"content": {% removeCommon item.templateContent | safe %}}
}{{ '' if loop.last else ',' }}
{%- endif -%}
{%- endfor %}
]
}
example from object (note: the title, in this example "p", seems to appear as the first element in the content: but doesn't appear in the title)
{
"title": "",
"url": "/html-element/p/",
"date": "Fri Jun 19 2020 09:59:37 GMT-0400 (Eastern Daylight Time)",
"content": p Defines paragraphs. Notes Define separate paragraphs with <p>, do not use a <br> to break up a paragraph Do not have empty <p>}
},
There's not much I can do if I can't replicate the issue via a test repo. If you could create one, that would help a lot.
This is my way of saying "If you can't set up a test repo, there's not much I can do to help you."
I need to look at your entire setup. I can't fix the problem if I can't find the cause.
I'm not sure why you 're unable to set up a dummy repo. If you don't want to push your code publicly to GitHub, you can set up a private repository and give me access.
@binyamin I'll see what I can come up with. I'm using fully dynamic data sources that won't be available from outside our internal netwek, so I'll have to use dummy JSON and change a bunch to get it to be functional.
So, I'll still create a test case, but I was able to get it all working with renderData - populating a page title and populating collections.all, which was the primary issue.
@jasonday are you still interested in this issue, or should I close it?
@jasonday are you still interested in this issue, or should I close it?
Sorry, I haven't had time to create a test case and ended up using renderData, which did work.
@jasonday be careful, renderData is deprecated, it will be removed in the future.
Hi.
I'm also having a hard time understanding why some eleventyComputed properties don't work while others do.
In my case, I'm trying to populate json-ld schema in my layout file with the data from the template. The date is coming from the global data file _data/objave.js.
I have this in my template's front matter:
layout: layouts/page.njk
pagination:
data: objave
size: 1
alias: objava
eleventyComputed:
page_title: "{{ objava.title }}"
page_description: "{{ objava.excerpt }}"
page_type: "article"
objava_image: "https://media.graphcms.com/{{ objava.photo }}"
objava_text: {{ objava.bodytext }}
objava_author: {{ objava.author }}
For some reason, while everything is rendering correctly in a template, objava_text and objava_author are rendering in the layout file as [object Object]. I have no issues with the other four properties, and this is something that I'm battling with in several of my eleventy projects - how to figure out which eleventyComputed property will give me grief and why.
Thank you for your time and effort helping me.
Cheers.
For some reason, while everything is rendering correctly in a template,
objava_textandobjava_authorare rendering in the layout file as[object Object].
@WebShapedBiz, you should put the strings within quotes (the way you are doing for others except objava_text and objava_author). Eleventy is treating the two pairs of curly braces as an object within an object.
eleventyComputed:
page_title: "{{ objava.title }}"
page_description: "{{ objava.excerpt }}"
page_type: "article"
objava_image: "https://media.graphcms.com/{{ objava.photo }}"
objava_text: "{{ objava.bodytext }}"
objava_author: "{{ objava.author }}"
Thank you @manustays, I'll try that.
I have similar problems with Nunjuck templates in computed data properties. Some values from the data cascade seem to be accessible (e.g. title) while others don’t (e.g. collections). I setup a test repository to demonstrate my issue. If I use javascript front matter in src/posts/posts.njk everything is working as expected.
dweidner/eleventy-computed-front-matter
I also tried the current master branch, same problems. Could it be that collections are simply not yet available at that point of time?
Giving unexpected results:
---
layout: archive.njk
title: Posts
tags: false
permalink: "/"
eleventyComputed:
total: "{{ collections.post | length }}"
---
<dl>
<dt>EleventyComputed</dt>
<dd>{{ total }}</dd>
<dt>Template</dt>
<dd>{{ collections.post | length }}</dd>
</dl>
Works as expected:
---js
{
layout: 'archive.njk',
title: 'Posts',
tags: false,
permalink: '/',
eleventyComputed: {
total({collections}) {
return collections.post.length;
}
}
}
---
<dl>
<dt>EleventyComputed</dt>
<dd>{{ total }}</dd>
<dt>Template</dt>
<dd>{{ collections.post | length }}</dd>
</dl>
I have similar problems with Nunjuck templates in computed data properties. Some values from the data cascade seem to be accessible (e.g.
title) while others don’t (e.g.collections). I setup a test repository to demonstrate my issue. If I use javascript front matter insrc/posts/posts.njkeverything is working as expected.dweidner/eleventy-computed-front-matter
I also tried the current master branch, same problems. Could it be that collections are simply not yet available at that point of time?
Giving unexpected results:
--- layout: archive.njk title: Posts tags: false permalink: "/" eleventyComputed: total: "{{ collections.post | length }}" --- <dl> <dt>EleventyComputed</dt> <dd>{{ total }}</dd> <dt>Template</dt> <dd>{{ collections.post | length }}</dd> </dl>Works as expected:
---js { layout: 'archive.njk', title: 'Posts', tags: false, permalink: '/', eleventyComputed: { total({collections}) { return collections.post.length; } } } --- <dl> <dt>EleventyComputed</dt> <dd>{{ total }}</dd> <dt>Template</dt> <dd>{{ collections.post | length }}</dd> </dl>
Did you end up solving this using Nunjucks?
Yes, that was the only solution that worked for me. I don't know if that has changed in the latest releases though.
@jasonday to access computed variables, use:
{{ eleventyComputed.title }}If you're also sometimes using
{{ title }}, then do this:{{ title or eleventyComputed.title }}
Is this by design? I figured it out via trial-and-error, but somehow the documentation had left me with an assumption that the eleventyComputed contents would be "woven in" and override page data.