Skip to content

Access the Data Cascade in filters and shortcodes #2844

@mbforbes

Description

@mbforbes

I was trying to use data from the Data Cascade in a filter. I asked about it in this discussion: #2841 and @pdehaan extremely helpfully solved it.

The relevant docs I could find are here:
https://www.11ty.dev/docs/filters/#scoped-data-in-filters

which say:

A few Eleventy-specific data properties are available to filter callbacks.

  • this.page Added in v2.0.0
  • this.eleventy Added in v2.0.0
module.exports = function(eleventyConfig) {
  // Make sure you’re not using an arrow function here: () => {}
  eleventyConfig.addFilter("myFilter", function() {
    // this.page
    // this.eleventy
  });
};

However, I think this documentation could be improved:

  1. There are more data properties available. For example, using Nunjucks, this.ctx is available. Critically, it contains the entire Data Cascade (I think). Perhaps we could list all the properties that are available?
  2. There are properties available before v2.0.0. Because both the properties listed are tagged as v2.0.0, I thought those were the only ones. Since I was on v1.0.0, I didn't think the arrow function distinction would affect me, but it does.
  3. The properties that are available depend on the filter. From @pdehaan, they're available at:
    • Nunjucks: this.ctx
    • Liquid: this.context.environments
    • Eleventy: (must pass in manually)

I think it's because I'm bad at JavaScript, but I find it very difficult to figure out what information I can access when I'm working inside filters or templating languages. Having documentation that lists out variables more exhaustively would help me out a lot. (I run into this genre of issue frequently working on my Eleventy + Nunjucks site.)

Many thanks for your consideration!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions