-
|
Hi, I'm migrating my Zola site to 11ty, and I need to solve a problem that I solved with a particular Zola feature that 11ty seems to lack. I'll first describe the feature, to see if it's possible to do that in 11ty, and then describe the problem, to see if there is some better 11ty solution for it. The feature: nth, OrdinalZola has a variable named
docs: https://www.getzola.org/documentation/content/shortcodes/#nth-invocation-count Hugo has a similar feature:
docs: https://gohugo.io/methods/shortcode/ordinal/ My usecaseFrom Hugo docs:
That's basically what I'm trying to accomplish. The important part is this JavaScript inside a window.addEventListener("load", () => {
const mastoTimeline{{ nth }} = new MastodonTimeline.Init({
mtContainerId: "mt-container{{ nth }}",
...
}and this HTML tag: <div id="mt-container{{ nth }}" class="mt-container">
...
</div>The I don't need the
Is there any way to implement the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Personally I would go for unique ID's. If you use something like uuid you're pretty much guaranteed to have a unique ID each time. That doesn't solve your second point though, they would be different on each build. Unless your JS uses these unique id's to create an That said, you could do what you want with something like this; export default (eleventyConfig) => {
// Set a counter for the instances of the shortcode.
let instanceCount = 0;
// Reset the counter before each build otherwise they will continue to increment.
eleventyConfig.on("eleventy.before", async ({ directories, runMode, outputMode }) => {
instanceCount = 0;
});
// Define the shortcode.
eleventyConfig.addShortcode("insertMasterdonTimeline", function () {
instanceCount += 1;
// Return whatever JS code you want to insert into the template with the counter value.
// You will need to wrap in the code below in <script> for it to output JS into the template.
// It is left out here so you can see the output in HTML for clarity.
return `
window.addEventListener("load", () => {
const mastoTimeline${instanceCount} = new MastodonTimeline.Init({
mtContainerId: "mt-container${instanceCount}"
})
})
`
}
);
}And then in your template <p>{% insertMasterdonTimeline %}</p>
<p>{% insertMasterdonTimeline %}</p>
<p>{% insertMasterdonTimeline %}</p>
<p>{% insertMasterdonTimeline %}</p>
<p>{% insertMasterdonTimeline %}</p>This will work for a single page. If you're using the shortcode on multiple pages you'd need to pass the page name into the shortcode. And |
Beta Was this translation helpful? Give feedback.
Personally I would go for unique ID's. If you use something like uuid you're pretty much guaranteed to have a unique ID each time. That doesn't solve your second point though, they would be different on each build. Unless your JS uses these unique id's to create an
id="uniqueID"in your HTML that someone can link to, it probably won't be an issue.That said, you could do what you want with something like this;