11ty / eleventy-plugin-webc

Adds support for WebC *.webc files to Eleventy
https://www.11ty.dev/docs/languages/webc/
120 stars 10 forks source link

Access page level data in WebC #30

Closed cfjedimaster closed 1 year ago

cfjedimaster commented 2 years ago

Consider the following where I define a page level variable in EJS -

<template webc:type="11ty" 11ty:type="ejs">
<%
name = "ray";
%>

</template>

Should I be able to access this in a webc file? I tried this:

<template webc:type="11ty" 11ty:type="ejs">
<%
name = "ray";
%>

</template>

<p>
I'm a cat.
</p>
<p @text="name"></p>
<p @html="name"></p>
<p :html="name"></p>
<p :title="name">title</p>

<hr>

And nothing seemed to work, but it could be that name is scoped to the template block and not used again. FYI, I realize I could just output from my EJS block, but was curious if I could use EJS to do computation type stuff, and 'regular' webc code output for the rest of the template.

As an aside (and if this should be a second issue, let me know). In the same file, I tried this on top:

---
othername: camden
---

and the front matter wasn't parsed at all.

zachleat commented 1 year ago

Fwiw, a few things here:

  1. front matter isn’t supported by components in your components folder, it’s only on WebC input templates.
  2. webc:type="11ty" doesn’t export any data or variables—it’s merely to render template syntax. You can define variables in there for use in the same webc:type="11ty" template node, but those are locally scoped.
  3. You might be happy to see https://github.com/11ty/webc/issues/109 (coming in WebC 0.9.0, releasing soon) which will export data to your component and https://github.com/11ty/eleventy-plugin-webc/issues/45 which unlocks javascript in front matter.
zachleat commented 1 year ago

This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.

If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and we will reopen the issue. Thanks!