Passing values to component? #3188
-
Currently I have a post layout that is imported in my frontmatter using this alias = Can I also create components that I can directly pass values to? and import inside my actual post? Something like = I don't wanna copy and paste the style every time and want to use this formatting on different texts throughout. That possible? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 10 replies
-
Oh and if this is possible, can I create aliases for it to easily include it? |
Beta Was this translation helpful? Give feedback.
-
Okay, here's some code which'll make it easier for you understand what I want.
Where my card.html looks like this:
Right now, I'm just putting the whole card.html thingy inside the |
Beta Was this translation helpful? Give feedback.
-
I know you can do something like that w/ LiquidJS templates, but not sure about .njk files. {% card post.url, post.data.title %} Or just pass the entire |
Beta Was this translation helpful? Give feedback.
-
If you're using Nunjucks and want to explicitly pass data along you can look at macros. |
Beta Was this translation helpful? Give feedback.
-
@pdehaan @edwardhorsford Alright, I looked at macros and shortcodes. They both work for my purpose. Thanks for the suggestions. Any reason why I should prefer one over the other? My order of preference:
|
Beta Was this translation helpful? Give feedback.
-
@pdehaan I've been using Nunjucks for far longer than I've been using Eleventy, so mostly it's because I'm more comfortable with them. I'm very used to using macros combined with filters (81 macros and 122 filters and counting). My reasons are below:
Some downsides:
@96312 I think both probably satisfy most of your requirements. And you're probably not locked in if you change your mind. Shortcodes are more js based, and macros html/templating based - and so it partly depends what you wan to use them for. The usage you've indicated is exactly what they're intended for, but shortcodes will also work well for it too. Not so related to this discussion which I expect is about reusable bits of template across an app - but macros can also be helpful for just a single page - if I've got a repeated element on a single page I may often make a macro out of it declared at the top of the page. |
Beta Was this translation helpful? Give feedback.
-
You can of course mix your approaches. I actually made my first shortcode the other week - one for rendering markdown in a Nunjucks template (where I explicitly don't want the template as a whole to be markdown). Using the shortcode the syntax ends up very nice like: {% markdown %}
## A second level heading
{% endmarkdown %} The equivalent macro would be: {% call markdown() %}
## A second level heading
{% endcall %} But then the macro would just have to pass the content out to a filter or a Nunjucks global so that you can use js to render the markdown. A shortcode is much cleaner / simpler. |
Beta Was this translation helpful? Give feedback.
-
If I might chime in, there's also another method, using 'include'. If you want to achieve what you showed originally, i.e.,
You can do this:
And then you would refer to postinfo.url and postinfo.data.title in your component. And if 'post' is already available in the context of every place you want to do the include, then you do not need the 'set' statement. Note that I ran into something earlier today where my include contained a reference to an async filter, which won't work inside of an include. The fix was to set a variable that used the filter prior to the include so that it would be available in the include. Hope that makes sense. |
Beta Was this translation helpful? Give feedback.
@pdehaan I've been using Nunjucks for far longer than I've been using Eleventy, so mostly it's because I'm more comfortable with them. I'm very used to using macros combined with filters (81 macros and 122 filters and counting). My reasons are below:
name(...args)
overname arg, arg
{%
is used for expressions, whilst{{
is used to render to page. Eleventy using{%
just feels 'wrong' to me.