-
I'd like to use the filename for each post in my template. Something like I hoped the following would work but it doesn't... {% assign posts = collections.how-are-you %}
{%- for post in posts -%}
<div>{{ post }}</div>
{%- endfor -%} the following does work: (printing {% assign posts = collections.how-are-you %}
{%- for post in posts -%}
<div>{{ post.data.title }}</div>
{%- endfor -%} but trying to print {% assign posts = collections.how-are-you %}
{%- for post in posts -%}
<div>{{ post.data }}</div>
{%- endfor -%} does not work. Trying to print My questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Buckle up, it's about to get educational up in here! First let's walk through the output of your attempts so far and see if we have roughly the same results. You said the first attempt "didn't work", but never mentioned what the output was. <h2>OPTION 1</h2>
{% assign posts = collections.how-are-you %}
{%- for post in posts -%}
<div>{{ post }}</div>
{%- endfor -%} When I run this, I get the following: <h2>OPTION 1</h2>
<div>[object Object]</div>
<div>[object Object]</div>
<div>[object Object]</div>
<div>[object Object]</div> That is also the same output I see if I try logging <h2>OPTION 2</h2>
<div>[object Object]</div>
<div>[object Object]</div>
<div>[object Object]</div>
<div>[object Object]</div> You mentioned using <h2>OPTION 3</h2>
{% assign posts = collections.how-are-you %}
{%- for post in posts -%}
<div>{{ post.data.title }}</div>
{%- endfor -%} <h2>OPTION 3</h2>
<div>FOUR</div>
<div>ONE</div>
<div>THREE</div>
<div>TWO</div> Where my ./src/how-are-you/one.liquid file looks something like this: ---
title: ONE
---
<h1>{{ title }}</h1> So I think by now we know our collection is there, has the expected items/count, we just don't know what We can try using Liquid's excellent "json" filter to output those objects as JSON: {%- for post in posts -%}
- <div>{{ post }}</div>
+ <div>{{ post | json }}</div>
{%- endfor -%} Now we should see something roughly close to the following: [11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] > Having trouble rendering liquid template ./src/how-are-you/index.liquid
`TemplateContentRenderError` was thrown
[11ty] > Converting circular structure to JSON
--> starting at object with constructor 'Liquid'
| property 'parser' -> object with constructor 'Parser'
--- property 'liquid' closes the circle, file:./src/how-are-you/index.liquid, line:9, col:6
`RenderError` was thrown
[11ty] > Converting circular structure to JSON
--> starting at object with constructor 'Liquid'
| property 'parser' -> object with constructor 'Parser'
--- property 'liquid' closes the circle
`TypeError` was thrown:
[11ty] TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Liquid'
| property 'parser' -> object with constructor 'Parser'
--- property 'liquid' closes the circle
at JSON.stringify (<anonymous>)
at Object.json (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:2938:17)
at Filter.render (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:2419:26)
at Value.<anonymous> (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:2455:49)
at step (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:87:23)
at Object.next (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:68:53)
at reduce (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:1058:25)
at Object.then (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:1026:43)
at reduce (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:1065:40)
at toThenable (/Volumes/Dev/github/11ty/11ty-2284/node_modules/liquidjs/dist/liquid.node.cjs.js:1053:16)
[11ty] Wrote 0 files in 0.03 seconds (v1.0.0) OK, so we still don't know what // .eleventy.js
const inspect = require("node:util").inspect;
module.exports = function (eleventyConfig) {
eleventyConfig.addFilter("inspect", function (obj = {}) {
return inspect(obj, {sorted: true});
});
return {
dir: {
input: "src",
output: "www",
}
};
}; And now we can use our custom <h2>OPTION 4</h2>
{% assign posts = collections.how-are-you %}
{%- for post in posts -%}
<div>{{ post.data | inspect }}</div>
{%- endfor -%} This will be a bit more verbose: <h2>OPTION 4</h2>
<div>{
collections: {
'how-are-you': [ [Object], [Object], [Object], [Object] ],
all: [ [Object], [Object], [Object], [Object] ]
},
eleventy: {
env: {
config: '/Volumes/Dev/github/11ty/11ty-2284/.eleventy.js',
root: '/Volumes/Dev/github/11ty/11ty-2284',
source: 'cli'
}
},
page: {
date: 2022-03-22T02:43:26.784Z,
filePathStem: '/how-are-you/four',
fileSlug: 'four',
inputPath: './src/how-are-you/four.liquid',
outputFileExtension: 'html',
outputPath: 'www/how-are-you/four/index.html',
url: '/how-are-you/four/'
},
pkg: {
author: 'Peter deHaan <peter@deseloper.com>',
description: '',
devDependencies: { '@11ty/eleventy': '^1.0.0' },
keywords: [],
license: 'MPL-2.0',
main: '.eleventy.js',
name: '11ty-2284',
scripts: {
build: 'eleventy',
test: 'echo "Error: no test specified" && exit 1'
},
version: '1.0.0'
},
tags: [ 'how-are-you' ],
title: 'FOUR'
}</div>
<div>...</div>
<div>...</div>
<div>...</div> It's pretty messy there, but we can see there is a
(Also see https://www.11ty.dev/docs/data-eleventy-supplied/ for more detailed explanations of most of these.) Or if we log out the <div>{
checkTemplateContent: true,
data: {
collections: { 'how-are-you': [Array], all: [Array] },
eleventy: { env: [Object] },
page: {
date: 2022-03-22T02:43:26.784Z,
filePathStem: '/how-are-you/four',
fileSlug: 'four',
inputPath: './src/how-are-you/four.liquid',
outputFileExtension: 'html',
outputPath: 'www/how-are-you/four/index.html',
url: '/how-are-you/four/'
},
pkg: {
author: 'Peter deHaan <peter@deseloper.com>',
description: '',
devDependencies: [Object],
keywords: [],
license: 'MPL-2.0',
main: '.eleventy.js',
name: '11ty-2284',
scripts: [Object],
version: '1.0.0'
},
tags: [ 'how-are-you' ],
title: 'FOUR'
},
date: 2022-03-22T02:43:26.784Z,
filePathStem: '/how-are-you/four',
fileSlug: 'four',
inputPath: './src/how-are-you/four.liquid',
outputPath: 'www/how-are-you/four/index.html',
template: Template {
behavior: TemplateBehavior {
config: [Object],
outputFormat: 'fs',
render: true,
write: true
},
computedData: ComputedData {
computed: {},
computedKeys: Set(0) {},
config: [Object],
declaredDependencies: {},
queue: [ComputedDataQueue],
symbolParseFunctions: {},
templateStringKeyLookup: {}
},
dataCache: {
eleventy: [Object],
page: [Object],
pkg: [Object],
tags: [Array],
title: 'FOUR'
},
extraOutputSubdirectory: '',
filePathStem: '/how-are-you/four',
fileSlug: TemplateFileSlug {
cleanInputPath: 'how-are-you/four.liquid',
dirs: [Array],
filenameNoExt: 'four',
inputPath: 'how-are-you/four.liquid',
parsed: [Object]
},
fileSlugStr: 'four',
frontMatter: {
content: '\n<h1>{{ title }}</h1>\n',
data: [Object],
excerpt: '',
isEmpty: false
},
inputContent: '---\ntitle: FOUR\n---\n\n<h1>{{ title }}</h1>\n',
inputDir: 'src',
inputPath: './src/how-are-you/four.liquid',
isDryRun: false,
isVerbose: true,
linters: [],
outputDir: 'www',
outputFormat: 'fs',
paginationData: {},
parsed: {
base: 'four.liquid',
dir: './src/how-are-you',
ext: '.liquid',
name: 'four',
root: ''
},
plugins: {},
serverlessUrls: null,
skippedCount: 0,
templateData: TemplateData {
config: [Object],
configApiGlobalData: [Object],
dataDir: 'src/_data',
dataTemplateEngine: false,
eleventyConfig: [TemplateConfig],
globalData: [Object],
inputDir: 'src',
inputDirNeedsCheck: true,
pathCache: [],
rawImports: [Object],
templateDirectoryData: [Object]
},
transforms: [],
wrapWithLayouts: true,
writeCount: 0
},
templateContent: [Getter/Setter],
url: '/how-are-you/four/'
}</div> |
Beta Was this translation helpful? Give feedback.
Buckle up, it's about to get educational up in here!
First let's walk through the output of your attempts so far and see if we have roughly the same results. You said the first attempt "didn't work", but never mentioned what the output was.
I tried my best to recreate your example locally so hopefully our code is somewhat close:
When I run this, I get the following:
That is also the same output I see if I try logging
{{ post.data }}
:<h2>OPT…