Display content or data from another page with a shortcode

It’s possible to display/import data from a page in another page with a shortcode, and it’s pretty cool! I didn’t find this type of usage, I hope that will be useful for other people :smiley:
Here an example:

Chapter 1:

---
title: Chapter 1
chapter: 1
description: "A description for the chapter 1"
---
Here some text.

Chapter 2:

---
title: Chapter 2
chapter: 2
description: "Few words about chapter 2"
---
Here some text.

Like the chapter 1 {{< cross-ref chapter="1" >}}.

Shortcode cross-ref.html:

{{ $chapter := .Get "chapter" | int }}
{{ range where .Site.Pages "Params.chapter" $chapter }}
<span><span>See {{ .Title }}</span><span>{{ .Description }}</span></span>
{{ end }}

Result for the chapter 2 page:

<h1>Chapter 2</h1>
<p>Here some text.</p>
<p>Like the chapter 1 <span><span>See Chapter 1</span><span>A description for the chapter 1</span></span>.

Important: don’t use variables like Summary, Content or ReadingTime, it will create an infinite loop (thanks @razon for the warning)!

4 Likes

Thanks for sharing, I would like to add a note to avoid using shortcode to get certain variables of other pages, such as Summary, Content, ReadingTime. There may be an infinite loop issue.

3 Likes

Thanks I just added a warning!