question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define global objects which can be accessed by render tag

See original GitHub issue

Having experienced a few cases where variables have unintentionally leaked into included template partials, it was interested to note that Liquid (and LiquidJS) now support the stricter render tag, and that include has been deprecated.

I tried to update my 11ty-generated site to use render, but in doing so, soon came across a limitation; global data objects are not available to rendered partials.

For example, I have the following partial:

<header class="banner">
  <div class="banner__container">
    <div class="banner__title">
      <a rel="home" href="/">{{ app.title }}</a>
    </div>
    <nav class="banner__navigation" aria-label="site">
      {%- render 'navigation' with navigation.primary -%}
      {%- render 'navigation' with navigation.secondary -%}
    </nav>
  </div>
</header>

The data for both app and navigation comes from global data files. Given the encapsulated nature of render, I have to declare these each time I wish to render this partial on a page, which is not always easy or possible within the confines of a Liquid template.

Looking at the relevant Shopify theme docs, I note that:

Global objects don’t need to be passed down. They are accessible from all files.

Shopify define their own global objects, but there’s no way for an author using LiquidJS to do the same, as far as I can see.

I imagine being able to define an array of globals when configuring LiquidJS (shown within the context of an 11ty configuration), like so:

const {Liquid} = require('liquidjs');
const app = require('./src/_data/app.json');
const navigation = require('./src/_data/navigation.json');

module.exports = function(eleventyConfig) {
  let options = {
    extname: '.liquid',
    strict_filters: true,
    root: [
      './src/_includes',
      './src/_layouts'
    ],
    globals: [
      app,
      navigation
    ]
  };

  eleventyConfig.setLibrary('liquid', new Liquid(options));
};

Alternatively, perhaps an object would allow authors to define a name for each global:

globals: {
  foo: app,
  bar: navigation
}

Not sure which is the best approach; I have no preference.

Is this a legitimate feature request, or have I missed something?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
harttlecommented, Jan 12, 2021

The globals can be set when creating anengine object.

let engine = new Liquid({
    root: path.join(__dirname, 'views'),
    extname: '.liquid'  // used for layouts/includes, defaults "",
    globals: {data: 'global'}
  })
1reaction
harttlecommented, Feb 6, 2020

At first glance it seems something contradictory: we need to make the variables passed down explicit, and we need globals at the same time. But I’ll try to be compatible with the shopify liquid and dig into that behaviour to make sure it’s intended. And I prefer to the second way (in which you can define a object literal to specify the globals).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global object - MDN Web Docs Glossary - Mozilla
A global object is an object that always exists in the global scope.
Read more >
How to Use a Globally Defined Script Inside a React Component
A global variable is defined inside a script tag independent of the React app. This global variable can be data that needs to...
Read more >
Global objects — Jinja Documentation
This section covers the behavior of global objects in the Jinja namespace and also the behavior of their attributes. Functions. Filters and Tests...
Read more >
How to create global variables accessible in all views using ...
I would like to be able to define variables like my site's title, description, author, etc in one place and have them accessible...
Read more >
Global keyword in Python - GeeksforGeeks
Variables that are only referenced inside a function are implicitly global. We use a global keyword to use a global variable inside a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found