Mix data (content of yml) with .Content

I wanted to try out the mix of Data (including shortcodes) with .Content in a blog post.

I created the following files/content:

# data/podcasts.yml
- title: "podcast 1"
  rating: 3
  www: https://example.org
  txt: |
    This is a long text
    with more lines

    and more

    {{< blockquote >}}
    and a shortcode [with a link](https://example.org).
    {{< /blockquote >}}
- title: "podcast 2"
- title: "podcast 3"
# layouts/blog/single.html
{{ define "main" }}
  {{ .Page.ReadingTime }}
  {{ .TableOfContents }}
  {{ .Content }}
  {{ if .Site.Data.podcasts }}
  {{ range sort .Site.Data.podcasts }}
    <h3>{{ .title }}</h3>
    {{ $opts := dict "display" "block" }}
    {{ .txt | $.Page.RenderString $opts }}
  {{ end }}
  {{ end }}
{{ end }}

This looks good so far. But the .Page.ReadingTime and .TableOfContents don’t contain the content of the yml file.

Is this even possible to mix/add? Can I overwrite .Content or add to it? Should I use a ScratchPad? Would a Content Adapter be a solution (although I don’t want to create “subpages”)?

Thanks a lot for any help, even if it’s just “no, this is not possible” so I can do it the “normal” way and not use Data :smiley:

Word count, reading time, etc. are based on .Content. The only way to change the content is to… change the content.

You could move this (or something like it) into a shortcode:

{{ if .Site.Data.podcasts }}
  {{ range sort .Site.Data.podcasts }}
    <h3>{{ .title }}</h3>
    {{ $opts := dict "display" "block" }}
    {{ .txt | $.Page.RenderString $opts }}
  {{ end }}

Then call the shortcode from your… content.

Ahh, thanks. I was under the (wrong) assumption, that I cannot use shortcodes since they would be somehow nested, but this seems to work.

Interestingly, the TableOfContents doesn’t include my shortcode headers. Could this be a bug?

I’m calling my shortcode via {{< podcasts >}} and my shortcode looks like this:

{{ if .Site.Data.podcasts }}
  {{ range sort .Site.Data.podcasts }}
    {{ $opts := dict "display" "block" }}
    {{ .title | $.Page.RenderString $opts }}
    {{ .txt | $.Page.RenderString $opts }}
  {{ end }}
{{ end }}

Thanks for your help in advance!

Use the {{% %}} notation.

Hm, I see what you mean but then I have the problem that the shortcodes in my yml will not be properly rendered. Thanks, I think I have to live with these headers not included in the TOC.

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