javascripteleventy

passing custom classes to a webc component/layout in eleventy


When building a site in eleventy I prefer to reserve the option to pass a custom class into the layout via custom front matter. I'm trying to figure out how to do this in the eleventy + webc ecosystem.

Usually I do something like this in the nunjucks layout:

<body class="{{ class }}">

And then just do this in the post or page front matter:

---    
class: "home"
---

I'm sure there's a pretty simple way of doing this for a webc world (where your layouts are also webc files) I'm just not sure how.


Solution

  • You would need to use a dynamic variable by prefixing it with a colon (:):

    ---
    class: "home:
    ---
    
    <body :class="class">
    

    Read more at the official documentation: https://www.11ty.dev/docs/languages/webc/#dynamic-attributes-and-properties