Outputting multiple variables into a shortcode #2624
-
|
Hi everyone, For simplicity sake, below is a really dumbed down version of my shortcode which outputs complex responsive image markup. When using this shortcode, is it possible to insert multiple variables as arguments into the altText variable? I want to output my shortcode like this: Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
You could probably use LiquidJS's ---
title: LiquidJS
eleventyComputed:
altComputed: "{{ someData.patient.procedureName }} after {{ someData.patient.treatmentNumber }} treatments"
---
<h1>{{ title }}</h1>
{%- capture alt -%}
{{ someData.patient.procedureName }} after {{ someData.patient.treatmentNumber }} treatments
{%- endcapture -%}
{%- respImage "a.jpg", alt %}
{%- respImage "b.jpg", altComputed %}
{%- respImage2 "c.jpg", someData %}---
title: Nunjucks
eleventyComputed:
altComputed: "{{ someData.patient.procedureName }} after {{ someData.patient.treatmentNumber }} treatments"
---
<h1>{{ title }}</h1>
{%- set alt -%}
{{ someData.patient.procedureName }} after {{ someData.patient.treatmentNumber }} treatments
{%- endset -%}
{%- respImage "a.jpg", alt %}
{%- respImage "b.jpg", altComputed %}
{%- respImage2 "c.jpg", someData %}/**
* @typedef {import('@11ty/eleventy/src/UserConfig')} EleventyConfig
* @typedef {ReturnType<import('@11ty/eleventy/src/defaultConfig')>} EleventyReturnValue
* @type {(eleventyConfig: EleventyConfig) => EleventyReturnValue}
*/
module.exports = function (eleventyConfig) {
eleventyConfig.addShortcode("respImage", function (src="", alt="") {
return `<img src="${ src }" alt="${ String(alt).trim() }" />`;
});
eleventyConfig.addShortcode("respImage2", function (src="", data={}) {
const { procedureName, treatmentNumber } = data.patient;
const alt = `${ procedureName } after ${ treatmentNumber } treatments`.trim();
return `<img src="${ src }" alt="${ alt }" />`;
});
return {
dir: {
input: "src",
output: "www",
}
};
};OUTPUT<h1>Nunjucks</h1>
<img src="a.jpg" alt="something after something2 treatments" />
<img src="b.jpg" alt="something after something2 treatments" />
<img src="c.jpg" alt="something after something2 treatments" /> |
Beta Was this translation helpful? Give feedback.
-
You pass only one parameter to a shortcode expecting at least 3, if I understood correctly... You may have 2 different ways to solve that type of trouble
|
Beta Was this translation helpful? Give feedback.
You could probably use LiquidJS's
captureor Nunjucks'setpaired tags, oreleventyComputedvalue, or just pass the global data file as an argument and doaltvalue in your shortcode function:--- title: LiquidJS eleventyComputed: altComputed: "{{ someData.patient.procedureName }} after {{ someData.patient.treatmentNumber }} treatments" --- <h1>{{ title }}</h1> {%- capture alt -%} {{ someData.patient.procedureName }} after {{ someData.patient.treatmentNumber }} treatments {%- endcapture -%} {%- respImage "a.jpg", alt %} {%- respImage "b.jpg", altComputed %} {%- respImage2 "c.jpg", someData %}