Created
October 17, 2019 23:43
-
-
Save scottwater/402a424eb9c32b6497cbcf6c5d012e62 to your computer and use it in GitHub Desktop.
See what data is available on a page in Eleventy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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