11ty / webc

Single File Web Components
MIT License
1.3k stars 36 forks source link

Unable to access collections from webc:setup with `EleventyRenderPlugin` #160

Closed zeroedin closed 1 year ago

zeroedin commented 1 year ago

I am also most likely trying to do something unsupported due to the use of the EleventyRenderPlugin:

index.njk

{% renderTemplate "webc" %}
  <item-list tag="foundations"></item-list>
{% endrenderTemplate %}

item-list.webc

<script webc:setup>
  function getCollectionItems(collections, tag) {
    const result = [...collections[tag]].reverse();
    return result;
  }
</script>

<ul>
  <li webc:for="item of getCollectionItems(collections, tag,
    <a :href="item.url" @raw="item.data.title"></a>
  </li>
</ul>
Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering njk template ./src/index.njk (via TemplateContentRenderError)
[11ty] 2. (./src/index.njk)
[11ty]   EleventyShortcodeError: Error with Nunjucks paired shortcode `renderTemplate` (via Template render error)
[11ty] 3. Check the dynamic attribute: `webc:for="getCollectionItems(collections, tag)"`.
[11ty] Original error message: Cannot read properties of undefined (reading 'foundations') (via Template render error)
[11ty] 
[11ty] Original error stack trace: Error: Check the dynamic attribute: `webc:for="getCollectionItems(collections, tag)"`.

If index.njk is renamed index.webc and removal of the renderTemplate block this works.

zachleat commented 1 year ago

My guess is that this relates to the global data access from inside child components breaking change in 0.11: https://github.com/11ty/webc/releases/tag/v0.11.0

Does <li webc:for="item of getCollectionItems($data.collections, tag, work?

zeroedin commented 1 year ago

Does <li webc:for="item of getCollectionItems($data.collections, tag, work?

It does! Thank you! Been poking at this all morning!