In Eleventy (11ty) the page source can define custom data in it's front-matter. E.g.:
---
title: this is front-matter data
---
Page content
{% myCustomTag valueOfArg1 %}
where the custom tag (also known as shortcode in Eleventy) is generating extra content based on a configured function handler:
eleventyConfig.addShortcode('myCustomTag', function(arg1) {
// how can I access here the page front-matter data? E.g. "title"
return `<div>...${ arg1 }...</div>`;
});
How can I access the page front-matter data in the code of a custom tag (shortcode)?
What I want to achieve is defining a custom tag (shortcode) that has optional arguments. If no arguments are supplied, the arguments are searched in the front-matter:
---
arg1: my value
---
Page content
{% myCustomTag %}
and the myCustomTag
handler function:
eleventyConfig.addShortcode('myCustomTag', function(arg1) {
// if arg1 missing, search page front-matter for arg1
// arg1 = arg1 || this.page.data.arg1 || 'default';
return `<div>...${ arg1 }...</div>`;
});