Data from the API available in the eleventy.config #3765
Replies: 2 comments 2 replies
-
I'm not sure you can. Can I ask why you need to use it in a |
Beta Was this translation helpful? Give feedback.
-
Ahhhh. I understand now. You'll be able to do something like: eleventyConfig.addShortcode("getTranslation", function ( language, label ) {
let data = this.ctx.headless
return return data.translations?[language]?[label] && data.translations[language][label] || `missing translation for ${label} in ${language}`
}) And then something like this in your template: {% getTranslation "en", "home" %} Note that your function when you add the shortcode has to be If you don't want to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a headless CMS with internationalization that will eventually feed my eleventy with data at build time.
The structure of the JSON below.
I created the file:
_data/headless.mjs
who fetches that from the external API.I generate my pages using the pagination like this and all works amazingly
I got stuck at writing something like:
<a href="#">{{ getTranslation("en", "home") }}</a>
eleventy.config.js
which is a lot cleaner and easier to manage than:
<a href="#">{{ headless.translations.en.home | default('missing') }}</a>
Beta Was this translation helpful? Give feedback.
All reactions