Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
See what data is available on a page in Eleventy
eleventyConfig.addNunjucksTag("pageData", function(nunjucksEngine) {
return new (function() {
this.tags = ["pageData"];
this.parse = function(parser, nodes, _) {
var tok = parser.nextToken();
var args = parser.parseSignature(null, true);
// fake it until you make it!
// https://github.com/mozilla/nunjucks/issues/158#issuecomment-34919343
if (args.children.length === 0) {
args.addChild(new nodes.Literal(0, 0, ""));
}
parser.advanceAfterBlockEnd(tok.value);
return new nodes.CallExtensionAsync(this, "run", args);
};
this.run = function(context, _, callback) {
// exclude the things we do not want
// and cause circular references (collections) :(
const { collections, pkg, scripts, ...pageData } = context["ctx"];
let ret = new nunjucksEngine.runtime.SafeString(
`<pre>${JSON.stringify(pageData, null, 2)}</pre>`
);
callback(null, ret);
};
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.