11ty / 11ty/webc

Computing data without render templates

Open
#164 6 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
1.4k
Forks
46
Avg merge
10m
Merged PRs (30d)
2

Description

Instigating nunjucks example:

{% set thingies = someComponentVar | expensivelyGenerateImages %}
<ul>{% for thingy in thingies %}
  <li data-index="{{thingies.getIndex(thingy)}}">
    <img alt="{{thingy.alt}}" src="{{thingy.src}}">
  </li>{% endfor %}
</ul>

If I have an expensive-but-private computation to do within (not without) a component, I'd like to do it in a script block. I don't want to use a render template though, because I prefer to use one templating language wherever possible

straw example:

<script type="module" webc:data>
export const thingies = await expensivelyGenerateImages(someCompVar);
</script>

<ul>
  <li webc:for="thingy of thingies" :data-index="thingies.getIndex(thingy)">
    <img :alt="thingy.alt" :src="thingy.src">
  </li>
</ul>

In the straw example, the webc:data script has access to and runs in the component scope, but you can easily imagine a case where you'd want to reference the computed private prop multiple times, and memoizing the calc function is just cumbersome.

<script type="module" webc:data>
export const likes = await getWebmentionLikes(page.url);
export const many = likes.length > 12;
</script>

<template webc:nokeep webc:if="likes.length">

<h2>Likes</h2>

<ul :class="`webmentions likes ${many ? 'many' : ''}`">
  <li webc:for="mention of likes" class="webmention like">
    <a target="_blank" rel="noopener" :href="mention.author.url">
      <img :src="mention.author.photo" :title="mention.author.name">
    </a>
  </li>
</ul>

</template>

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the webc:data examples and the component-scope behavior described in the issue, then trace the existing handling of webc:for, webc:if, and template webc:nokeep. Define how computed exports should be available to the surrounding component markup, including asynchronous values and repeated references; done means the proposed behavior is implemented and covered by representative examples or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.