Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow accessing globals from rendered liquid partials #3212

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vrugtehagel
Copy link
Contributor

@vrugtehagel vrugtehagel commented Feb 23, 2024

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 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

global data is not passed to liquidjs as globals
1 participant