-
|
Here are my codes.
productImage: async (src, alt) => {
const metadata = await Image(src, {
widths: [null],
formats: ['webp'],
outputDir: './dist/img/',
})
let imageAttributes = {
alt,
loading: 'lazy',
decoding: 'async',
}
return Image.generateHTML(metadata, imageAttributes)
},
{% for item in home.acf.home_builders %}
{% ifAsync item.acf_fc_layout == 'hero' %}
{% include "index/hero.njk" %}
{% elif item.acf_fc_layout == 'products' %}
{% include "index/products.njk" %}
{% endif %}
{% endfor %}
<div>
<div>
<h1 class="text-gradient mb-6">{{ item.title }}</h1>
<p class="text-2xl mb-6">{{ item.description }}</p>
</div>
<div>
{% for product in item.products %}
{% productImage product, "ALT TEXT" %}
{% endfor %}
</div>
</div>If you see the code Final Code {% for item in home.acf.home_builders %}
{# {{ item | dump }} #}
{% ifAsync item.acf_fc_layout == 'hero' %}
<div style="--bg-hero: url('{% bgImage item.background_image %}')" class="bg-[image:var(--bg-hero)] bg-no-repeat bg-contain bg-right-bottom">
<div class="flex items-center justify-between max-w-6xl mx-auto">
<div class="basis-2/5">
<h1 class="text-gradient mb-6">{{ item.title }}</h1>
<p class="text-2xl mb-6">{{ item.description }}</p>
<a class="btn" href="#contact">Level Up Today</a>
</div>
<div class="basis-3/5">
{% set img = item.image %}
{% heroImage img, "Wallet codes B2B", 849 %}
</div>
</div>
</div>
{% elif item.acf_fc_layout == 'products' %}
<div>
<div>
<h1 class="text-gradient mb-6">{{ item.title }}</h1>
<p class="text-2xl mb-6">{{ item.description }}</p>
</div>
<div>
{% for product in item.products %}
{% productImage product, "Wallet Codes Product" %}
{% endfor %}
</div>
</div>
{% endif %}
{% endfor %} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
|
Since your Nunjucks shortcode is async, I think you need to use https://mozilla.github.io/nunjucks/templating.html#asynceach instead of vanilla |
Beta Was this translation helpful? Give feedback.
-
|
@pdehaan it seems there is a bug with include keyword in nunjucks? My test code. {% asyncEach item in home.acf.home_builders %}
{# {{ item | dump }} #}
{% ifAsync item.acf_fc_layout === "hero" %}
{% include "index/hero.njk" %}
{% elseif item.acf_fc_layout === "products" %}
{% include "index/products.njk" %}
{% elif item.acf_fc_layout === "benefits" %}
{% include "index/benefits.njk" %}
{% elif item.acf_fc_layout === "process" %}
{% include "index/process.njk" %}
{% elif item.acf_fc_layout === "partners" %}
{% include "index/partners.njk" %}
{% elif item.acf_fc_layout === "about" %}
{% include "index/about.njk" %}
{% elif item.acf_fc_layout === "contact" %}
{% include "index/contact.njk" %}
{% endif %}
{% endeach %}It only load the Hero section. |
Beta Was this translation helpful? Give feedback.
-
|
I'm not seeing the same behavior locally with my presumably similar config: .eleventy.js/**
* @typedef {import('@11ty/eleventy/src/UserConfig')} EleventyConfig
* @typedef {ReturnType<import('@11ty/eleventy/src/defaultConfig')>} EleventyReturnValue
* @type {(eleventyConfig: EleventyConfig) => EleventyReturnValue}
*/
module.exports = function (eleventyConfig) {
eleventyConfig.addNunjucksAsyncShortcode("productImage", async (src="", alt="") => {
return Promise.resolve(`<img src="${src}" alt="${alt}" loading="lazy" />`);
});
return {
dir: {
input: "src",
output: "www",
}
};
};src/index.njk---
title: Index
layout: layout.njk
---
<h1>{{ title }}</h1>src/_includes/layout.njk<main>
{{ content | safe }}
</main>
<aside>{%- include "sidebar.njk" -%}
<footer>
<p>Powered by {{ eleventy.generator }}</p>
</footer>src/_includes/sidebar.njk{% asyncEach p in products %}
<article>
<!-- {{ p | dump(2) | safe }} -->
<h2>{{ p.title }}</h2>
<p>layout={{ p.acf_fc_layout }}</p>
{% productImage p.src, p.alt %}
<section data-note="dynamic include">
{% include p.acf_fc_layout + ".njk" ignore missing %}
</section>
<section data-note="if..elif">
{% if p.acf_fc_layout == "hero" -%}
{%- include "hero.njk" -%}
{% elif p.acf_fc_layout == "products" -%}
{%- include "products.njk" -%}
{% else -%}
<!-- unknown partial: "{{ p.acf_fc_layout }}" -->
{% endif -%}
</section>
</article>
{% endeach %}src/_data/products.jsmodule.exports = async () => {
return Promise.resolve([
{
title: "pRoDuCtS PaGe",
acf_fc_layout: "products",
src: "/images/products.png",
alt: "pRoDuCtS",
},
{
title: "HeRo PaGe",
acf_fc_layout: "hero",
src: "/images/hero.png",
alt: "HeRo",
},
{
title: "BeNeFiTs PaGe",
acf_fc_layout: "benefits",
src: "/images/benefits.png",
alt: "BeNeFiTs",
},
]);
};OUTPUT<main>
<h1>Index</h1>
</main>
<aside>
<article>
<!-- {
"title": "pRoDuCtS PaGe",
"acf_fc_layout": "products",
"src": "/images/products.png",
"alt": "pRoDuCtS"
} -->
<h2>pRoDuCtS PaGe</h2>
<p>layout=products</p>
<img src="/images/products.png" alt="pRoDuCtS" loading="lazy" />
<section data-note="dynamic include">
<p>pRoDuCtS iNcLuDe</p>
</section>
<section data-note="if..elif">
<p>pRoDuCtS iNcLuDe</p>
</section>
</article>
<article>
<!-- {
"title": "HeRo PaGe",
"acf_fc_layout": "hero",
"src": "/images/hero.png",
"alt": "HeRo"
} -->
<h2>HeRo PaGe</h2>
<p>layout=hero</p>
<img src="/images/hero.png" alt="HeRo" loading="lazy" />
<section data-note="dynamic include">
<p>HeRo iNcLuDe</p>
</section>
<section data-note="if..elif">
<p>HeRo iNcLuDe</p>
</section>
</article>
<article>
<!-- {
"title": "BeNeFiTs PaGe",
"acf_fc_layout": "benefits",
"src": "/images/benefits.png",
"alt": "BeNeFiTs"
} -->
<h2>BeNeFiTs PaGe</h2>
<p>layout=benefits</p>
<img src="/images/benefits.png" alt="BeNeFiTs" loading="lazy" />
<section data-note="dynamic include"></section>
<section data-note="if..elif">
<!-- unknown partial: "benefits" -->
</section>
</article>
<footer>
<p>Powered by Eleventy v1.0.2</p>
</footer>
</aside>So using Eleventy v1.0.2, I'm seeing the expected behavior using async shortcodes, and dynamic data using promises, and dynamic includes (or explicit |
Beta Was this translation helpful? Give feedback.
I'm not seeing the same behavior locally with my presumably similar config:
.eleventy.js
src/index.njk
src…