-
-
Notifications
You must be signed in to change notification settings - Fork 494
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
How do I print all data in a page? #1526
Comments
You could make a custom filter: // https://nodejs.org/api/util.html#util_util_inspect_object_options
const inspect = require("util").inspect;
module.exports = (eleventyConfig) => {
eleventyConfig.addFilter("debug", (content) => `<pre>${inspect(content)}</pre>`);
// ...
}; NOTE: Using Then, in your template, you can use it like this: <div>
{{ page | debug }}
</div> And then the output should be something similar to: <div>
<pre>{
date: 2020-11-22T21:56:37.799Z,
inputPath: './src/index.liquid',
fileSlug: '',
filePathStem: '/index',
url: '/',
outputPath: 'www/index.html'
}</pre>
</div> |
Thanks! That helps (and works). But it’s not printing something I wanted to check... From using the |
Can you give the markup you use for rendering? |
I think you want to pass the page context to the debug filter. You could try making a filter that will return you the context, and then passing that to the debug filter. |
index.html---
layout: default.pug
---
<pre><code>{{ page | debug }}</code></pre> default.pugdoctype html
html
head
meta(charset='utf-8')/
title Eleventy Test Site
body
h2 Eleventy Test Site
.
!{content}
(Wow…you all respond fast. Thank you! 🙏🏽) |
If it be global data that you want, you can always pass that global data object to your custom module.exports = [
"Billy",
"Bob"
]; ... then I should be able to dump that global data <h2><code>names</code> (global data)</h2>
<div>
{{ names | debug }}
</div> OUTPUT<h2><code>names</code> (global data)</h2>
<div>
<pre>[ 'Billy', 'Bob' ]</pre>
</div> I'm not sure how/if you can have 11ty dump ALL it's global data with a single command, or if the custom filter wouldn't have access to that scope/context. |
Eureeka! Using the filter you have provided (I could not have done it without your help!), and by inspecting
|
Wow, congrats! I never would have thought to inspect But that did inspire me to look for ---js
{
title: "Computed global data?",
eleventyComputed: {
datum(data) {
return data;
},
age() {
return new Date();
}
},
tags: ["ok computer"]
}
---
<h1>{{ title }}</h1>
{{ datum | debug }}
{{ age | debug }} That outputs the following to my generated index.html page: <h1>Computed global data?</h1>
<pre><ref *1> {
names: [ 'Billy', 'Bob' ],
pkg: {
name: '11ty-1526',
description: 'How do I print all data in a page? #1526',
version: '1.0.0',
author: 'Peter deHaan <peter@deseloper.com> (https://about.me/peterdehaan)',
bugs: { url: 'https://github.com/pdehaan/11ty-1526/issues' },
dependencies: {},
devDependencies: { '@11ty/eleventy': '^0.11.1' },
homepage: 'https://github.com/11ty/eleventy/issues/1526',
keywords: [],
license: 'MPL-2.0',
private: true,
repository: {
type: 'git',
url: 'git+https://github.com/pdehaan/11ty-1526.git'
},
scripts: {
build: 'eleventy',
test: 'echo "Error: no test specified" && exit 1'
}
},
title: 'Computed global data?',
eleventyComputed: { datum: [Function: datum], age: [Function: age] },
tags: [ 'ok computer' ],
page: {
date: 2020-11-22T21:56:37.799Z,
inputPath: './src/index.liquid',
fileSlug: '',
filePathStem: '/index',
url: '/',
outputPath: 'www/index.html'
},
collections: { all: [ [Object] ], 'ok computer': [ [Object] ] },
datum: [Circular *1],
age: 2020-11-26T17:54:22.522Z
}</pre>
<pre>2020-11-26T17:54:22.522Z</pre> |
If you feel like your question was answered, please close the issue. |
@Ryuno-Ki Okay, but… I would like to make an official feature request for an easy-access way to get at global data. Would you rather me close this an open a new Issue? I would just end up referencing this Issue anyways—which would just force people to follow the link here if they wanted to read up on the background context. If that is your preference, I’ll do it. But if you are okay with “converting” this Issue from a help request to a feature request, it might be helpful to have the explanation here in one place. |
Hm, good question. It's easier to read the first post to get an idea of what is asked about instead of walking through a thread. |
Sound reasoning. Will do! And thank you. 🙏🏽 |
I just want to print out all the data to look at how it’s structured.
I basically want to do the equivalent of a
console.dir(…)
orconsole.inspect(…)
, but print the data within a<pre>…</pre>
element.Can I do this?
How?
The text was updated successfully, but these errors were encountered: