Skip to content

Add collections access API for shortcodes #1818

Open
@cmcknight

Description

@cmcknight

Is your feature request related to a problem? Please describe.
I am trying to access a collection from a shortcode. While I can pass in the collection currently used by the template as an argument to the shortcode, I am unable to access other collections from within the shortcode.

Describe the solution you'd like
Ideally I would be able to use the following syntax:

eleventyConfig.addShortcode('getMfr', mfrId => {
  let mfr = collections.getItem('collection name', item => item.mfrId === mfrId)
  return mfr
})

Describe alternatives you've considered
My current workaround is to use the following:

  eleventyConfig.addShortcode("getMfr", mfrId => {
    let mfrData = JSON.parse(fs.readFileSync('./src/_data/manufacturers.json'))
    let mfr = mfrData.find(obj => obj.mfrId === mfrId)
    let text = `<a href="${mfr.url}" target="_blank">${mfr.name}</a>`
    return text;
  })

I've also tried:

const mfrData = require('./src/_data/manufacturers.json')

Both work, but they feel inelegant...

Additional context
My goal is to avoid the need for a backend database until it's really necessary. For building a static site, if it's not an Amazon.com, it's likely that it's unnecessary.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions