Allow accessing globals from rendered liquid partials
This is a PR that fixes https://github.com/11ty/eleventy/issues/1541, and can probably be extended to include https://github.com/11ty/eleventy/issues/3172 and https://github.com/11ty/eleventy/issues/2453 as well.
In a nutshell, the problem is that liquid partials rendered with the {% render %}
tag are encapsulated to the point they do not have access to global data configured through addGlobalData
, global data files, directory- and page-specific data, and Eleventy-provided globals such as page
. The only global data they have access to is that configured through LiquidJS's globals
option (passed to .setLiquidOptions()
).
This PR does not expose all of the data mentioned above to {% render %}
-style partials (though can be extended to do so), but instead only exposes data configured through addGlobalData
and global data files. LiquidJS's globals
option has the lowest priority in this data cascade (which is consistent with the {% include %}
equivalent).
A bit about the PR's contents; I've added a .needsGlobals()
method to TemplateEngine
instances. This method returns a boolean indicating whether it wants to receive all global data before rendering. Receiving the globals is not generally necessary, but as mentioned before, LiquidJS requires all global data be passed to its globals
option in order to make it available in partials renderd with {% render %}
. As such, by default, this .needsGlobals()
method returns false
; the Liquid
engine then overrides this method with one returning true
. Then, at the time the data is first retrieved, the necessary global data is passed to the engines that need it (for now, only the Liquid engine) using an addGlobals
method (one the Nunjucks engine already possesses).
I also added two tests; if a test with an actual data file is desired, I'd like some pointers on how to accomplish that neatly (I didn't see other tests doing something like that).