Eleventy: global data is not passed to liquidjs as globals

Created on 4 Dec 2020  路  2Comments  路  Source: 11ty/eleventy

Describe the bug
With {% render layout.html %}, you can only access a variable if it's defined in the included template/partial or passed down to liquid via liquidOptions.globals. 11ty refers to "global data", which is undefined within such partials.

To Reproduce
See example repo
If you don't feel like running the example repo:

  1. In a new 11ty project, reference a global data key from within an include/partial.
  2. In a markdown file, {% render %} the include/partial.
  3. Run npx eleventy

Expected behavior
I expect that data which is the same for every page (eg. pkg, _data/**/*) is passed down to liquid, whereas local variables are not.

Environment:

  • OS and Version: Xubuntu 20.04
  • Eleventy Version: 1.0.0-beta.0+sha:9042341 :wink:

Additional context
Related: https://github.com/harttle/liquidjs/issues/185

bug

Most helpful comment

I鈥檓 not a Liquid expert, so don鈥檛 quote me on this, but I believe this is intended. The documentation on render states:

When a snippet is rendered, the code inside it does not automatically have access to the variables assigned using variable tags within the snippet's parent template. Similarly, variables assigned within the snippet can't be accessed by the code outside of the snippet. This encapsulation increases performance and helps make theme code easier to understand and maintain.

My understanding of this is that by definition, render does not pass through its current scope to the partial (which is overall a good thing, as they explain). Any variable that requires reading needs to be passed as an argument to the render call. In your case:

{% render "foo.html", bar: bar %}

Edit: If you would like to circumvent that issue, I believe you could use the deprecated include tag which does share scopes between the parent and the child templates. The 11ty docs use include everywhere by the way.

All 2 comments

I鈥檓 not a Liquid expert, so don鈥檛 quote me on this, but I believe this is intended. The documentation on render states:

When a snippet is rendered, the code inside it does not automatically have access to the variables assigned using variable tags within the snippet's parent template. Similarly, variables assigned within the snippet can't be accessed by the code outside of the snippet. This encapsulation increases performance and helps make theme code easier to understand and maintain.

My understanding of this is that by definition, render does not pass through its current scope to the partial (which is overall a good thing, as they explain). Any variable that requires reading needs to be passed as an argument to the render call. In your case:

{% render "foo.html", bar: bar %}

Edit: If you would like to circumvent that issue, I believe you could use the deprecated include tag which does share scopes between the parent and the child templates. The 11ty docs use include everywhere by the way.

@HugoGiraudel thanks, I've seen that in the docs. I guess this is really a question of whether _eleventy's_ global data should be passed down as _liquidjs's_ global data.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

veleek picture veleek  路  3Comments

matt-auckland picture matt-auckland  路  3Comments

zachleat picture zachleat  路  3Comments

zellwk picture zellwk  路  3Comments

aaronstezycki picture aaronstezycki  路  3Comments