Pass data object with page
property to data method of JavaScript templates
#723
Labels
page
property to data method of JavaScript templates
#723
Is your feature request related to a problem? Please describe.
I recently discovered that Eleventy supports layout chaining. What a powerful feature! I put a JavaScript template in front of my Nunjucks template and was able to dynamically create an array of images in each of my pages' directories. I did this by calling
fs.readdirSync
in therender
method of my JS template.However, I ran into an issue of compilation order, where I tried to access these
images
arrays in the template of an index page. This didn't work, because therender
of those pages had not been run yet.The
data
method had been run, though. And after a bit of digging in the Eleventy code, I concluded that it looks like it would be fairly simple to run my existing logic in thedata
method of my JavaScript templates, if the existing front matter data was passed to that method.Describe the solution you'd like
Basically, if a change could be made to
Template#getData
whereTemplateContent#getFrontMatterData
is called after the other methods for retrieving front matter data and we also pass a merged data object toTemplate#getData
.Perhaps the entire data object isn't even needed, but the
data.page
object definitely is, since I use thedata.page.inputPath
to read the contents of the current directory in mydata
method.Describe alternatives you've considered
My suggested solution seems like a very simple change to the existing architecture, as opposed to implementing some solution for controlling the order in which pages are rendered, for instance.
Additional context
Here is my existing JavaScript template code, and an example of what it could look like if this change was implemented.
#642 also seems like a related issue, but in the context of JavaScript data files rather than JavaScript templates.
project.11ty.js
If this change was implemented:
The text was updated successfully, but these errors were encountered: