Extract data from shortcodes inside another page

I wonder if it’s possible to extract data from few shortcodes from different pages and put it in another page.
I have made several searches without luck, maybe I didn’t look hard enough.
Here is an example of what I have in mind:

First page’s source

---
title: First page
---
Some text.

{{< extr title="Extract 1" id="ex01" >}}
About extract 1.
{{< /extr >}}

Second page’s source

---
title: Second page
---
Some text, again

{{< extr title="Extract 2" id="ex02" >}}
About extract 2.
{{< /extr >}}

Rendering of the extracts page

(In another page, with a dedicate template or a partial.)

<section>
  <h1>All the extracts</h1>
	<ul>
	  <li id="ex01"><strong>Extract 1</strong>: About extract 1</li>
		<li id="ex02"><strong>Extract 2</strong>: About extract 2</li>
	</ul>
</section>

Take this for a spin. Extract summary is in the home page template.

git clone --single-branch -b hugo-forum-topic-43384 https://github.com/jmooring/hugo-testing hugo-forum-topic-43384
cd hugo-forum-topic-43384
hugo server
1 Like

Wahoo! I dit not know .HasShortcode (source), can we say it is a hack in your implementation? Whatever it is, it is great! Huge thanks!
Just a question: {{ $noop := .WordCount }} is not useful here?
I will write a post about what I can do with that (in few weeks).

No, it is not a hack.

Good question, and I’m not sure of the answer.

Using .Scratch or .Store to pass data between pages, or even within the same page, depends on rendering order, and Hugo renders pages in parallel.

If we need a Scratch or Store value from a page that has not been rendered, we can force the page to be rendered by doing something like:

{{ $ foo := .Content}}

There are other page methods that force the content to be rendered, including .WordCount.

The .HasShortcode method may force the page to be rendered; I am not sure. Including the noop assignment certaintly doesn’t hurt.

EDIT: I just tested this using hugo server with and without the noop assignment. Keep it.

1 Like

For the noop I don’t see the difference but I believe you, and thanks for the explanation.

I have another question: how can I retrieve other data/informations from the shortcode (in addition to the inner)? Like a title or an id, some parameters in my first example. I’m sure it’s possible but I don’t find the right way.

See https://gohugo.io/templates/shortcode-templates/#access-parameters

If you have additional questions, please open a new topic. Thanks.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.