Allow accessing globals from rendered liquid partials #3212
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a PR that fixes #1541, and can probably be extended to include #3172 and #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 throughaddGlobalData
, global data files, directory- and page-specific data, and Eleventy-provided globals such aspage
. The only global data they have access to is that configured through LiquidJS'sglobals
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 throughaddGlobalData
and global data files. LiquidJS'sglobals
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 toTemplateEngine
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 itsglobals
option in order to make it available in partials renderd with{% render %}
. As such, by default, this.needsGlobals()
method returnsfalse
; theLiquid
engine then overrides this method with one returningtrue
. 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 anaddGlobals
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).