How to Use Output from Shortcode test1 as Input for Shortcode test2 in Markdown

Hi,

I hope you’re doing well. I’m working on a Markdown file and I’m trying to figure out how to use the output of one shortcode as the input for another. Specifically, I want to take the output from a shortcode test1 and use it as the input for another shortcode test2. I’m not sure how to implement this within a Markdown file.

Example of What I’m Trying to Achieve

Let’s say I have the following shortcodes:

md

{{< test1 param1="value1" param2="value2" >}}

This would output something like:

md

347

Now, I want to use output-from-test1 as the input for test2:
md

{{< test2 input="347"  params="someparams">}}

So, my goal is to combine these two shortcodes like this:

md

{{< test2 input="{{< test1 param1='value1' param2='value2' >}}"  params="someparams" >}}

Expected Behavior

The test1 shortcode should generate its output, and then test2 should use that generated output as its input.

Questions

  1. Is this possible within the Markdown framework I’m using?
  2. If yes, could you please provide guidance or an example on how to achieve this?

Thank you for your assistance!

Best regards,
Ilya

That’s not possible from the markdown file.

You may use a Scratch | Hugo

  • save the data from first shortcode there.
  • recall it in the second one

Thank you a lot,
While it looks complicated and voluminous, I would like to find a more universal solution, maybe this is it, I will study it. Please tell me, if you have any more information for me

you will need to read about Custom Shortcodes, templating language, Context, Scratch, …

So I’m unsure what to tell you cause I don’t know how deep you are in.

here’s a basic starter using shortcodes with positional parameters to study while browsing the docs.

Try it out, read the docs play around.

Hope that’s what you have been looking for.

index.md

# store the result in _variable_ var1
{{< add var1 2 4 >}}

# store the result in _variable_ var2
{{< add var2 3 2 >}}

# print formatted result of multiplying values _var1_ and _var2_ 
{{< mul "var1" "var2" "Result: %d" >}}

shortcodes for mul and add

the mul and add within the shortcodes are the standard functions from Math functions | Hugo

layouts/shortcodes/add.html
{{ $varname := .Get 0 }}
{{ $sum1 := .Get 1 }}
{{ $sum2 := .Get 2 }}

{{ $result := add $sum1 $sum2 }}

{{ .Page.Scratch.Set $varname $result }}

<p>adding of {{ $sum1 }} and {{ $sum2 }} results in {{ $result }}</p>
layouts/shortcodes/mul.html
{{ $var1 := .Get 0 }}
{{ $var2 := .Get 1 }}
{{ $formatString := .Get 2 }}

{{ $mul1 := .Page.Scratch.Get $var1 }}
{{ $mul2 := .Page.Scratch.Get $var2 }}

{{ $result := mul $mul1 $mul2 }}

<p>{{- printf $formatString $result -}}</p>

will lead to

<p>adding of 2 and 4 results in 6</p>
<p>adding of 3 and 2 results in 5</p>
<p>Result: 30</p>
1 Like