Pass arguments to a WebC component? #3396
Replies: 3 comments 1 reply
-
Hi @VAggrippino! It seems the main problem in the code you shared is where you are trying to access I created a repo from my new minimal WebC starter for this discussion at https://github.com/rdela/webcbed-eleventy-3396 That is deployed to Netlify at https://webcbed-3396.netlify.app/ This is the component code https://github.com/rdela/webcbed-eleventy-3396/blob/trunk/_components/collections-post.webc |
Beta Was this translation helpful? Give feedback.
-
This was much simpler than I was making it out to be. The value I wanted to pass worked just fine when passed like a regular HTML attribute. The code below works fine ... with one caveat. At one point I was working on this code using an old computer (2014 model HP laptop) and I ran into a
<collections-post numposts="2">
<script webc:setup>
const recent_posts = (n) => {
const posts = collections.post
const sorted = posts.sort((a, b) => a.page.date < b.page.date ? -1 : 1)
const top = sorted.slice(0 - n)
return top.reverse()
}
</script>
<article class="collections-post" webc:for="post of recent_posts(numposts)">
<h3 class="collections-post__title"><a :href="post.data.page.url" @text="post.data.title"></a></h3>
<div class="collections-post__date" @text="formatted_date(post.data.page.date)"></div>
<div class="collections-post__content" @html="post.content"></div>
</article> |
Beta Was this translation helpful? Give feedback.
-
😖 Oh brother! ... In my actual code, the component has a I think this is a new question... Why does scoped CSS break props? Update: WebC scoped styles breaks Props? |
Beta Was this translation helpful? Give feedback.
-
Is it possible to pass something like an argument to a WebC component?
I'm building a site with a blog and several pages that aren't blog posts. On the home page (
/index.html
) I want to show the latest couple of blog posts and on/blog/index.html
I want to show the latest 5 posts. I created a single component that I thought I could use on either page. I usedwebc:setup
to define a function that returns just the desired posts fromcollections.post
andwebc:for
to render the posts.It works, but it renders all of the posts instead of just the ones I wanted. It should generate an error of some kind since it depends on a variable that is apparently
undefined
.I thought Properties was my solution, but it's not working for me.
/_components/collections-post.webc
:/index.webc
:If I use the example found in the documentation ("
@prop="hello"
"), it works perfectly, but that numposts paragraph in my example above just renders as an empty paragraph.Beta Was this translation helpful? Give feedback.
All reactions