How to add to .Content before rendering?

Hello,
I’m trying to set up FAQ schema and everything works pretty good, except of generating ToC for my file.

My setup is pretty simple, in my FAQ files I’m using this YAML:

faq:
  title: Just optional title
  section:
    - question: What is this?
      answer: This is **bold** answer
    - question: What is this?
      answer: This is *italic* answer
    - question: Third Question
    - answer: Third Answer

Then, in my FAQ template I’ve added this:

{{ with $.Page.Params.faq }}
			{{ $len := len .section }}
				{{ range $index, $element := .section }}		
					{{ .question | markdownify }} 
					{{ .answer | markdownify }}	
				{{ end }}
			{{ end }}

And it works, but it doesn’t generate ToC properly, because it’s not added to .Content variable before generating ToC. I hope it makes sense:

  {{- if (.Param "ShowToc") }}
  {{- partial "toc.html" . }}
  {{- end }}
  {{- if .Content }}
  <div class="post-content">
    {{- if not (.Param "disableAnchoredHeadings") }}
    {{- partial "anchored_headings.html" .Content -}}
    {{- else }}
		{{ .Content }}
		{{ end }}
  </div>
  {{- end }}

How should I modify my loop, so it would “print” the .question and .answer, but instead just add it to .Content before rendering it?

Not sure to understand what you want to do, but there is a **{{.TableOfContents}}** variable available.

see Table of Contents | Hugo

Posts using FAQ layout have two parts:

  • introduction
  • questions and answers

When I publish FAQ - ToC is automatically generated for me (I’m using partial for it - toc.html). It works for the first part (Introduction), because it takes current context to generate it:

{{- partial "toc.html" . }}

Second part of my post (actual FAQ) is not part of .Content block / context, because it’s above Front Matter in content file (faq.md). I print question / answer with the loop.

Instead of printing questions / answers I would like to first add it to .Content, so when I do:

{{ .Content }}

I would get first and second part of the post (introduction and Q/A) together. It would also mean, that when I’m calling:
{{- partial "toc.html" . }}
I generate ToC for Q/A as well. At this point, toc.html partial cannot see h2 headers in Q/A part, because I’m not passing them through (only .Content is being passed to toc.html).

.Content is the rendered markdown (defined below the front matter) in your content file. It is an immutable value.

If you want to include front matter within .Content, you must use a shortcode.

If you want the heading elements of a rendered shortcode to be included in the .TableOfContents, you must call the shortcode using the {{% my-shortcode %}} notation.

3 Likes

Thank you @jmooring !

Your solution worked. I’ve added shortcode faq in my content file.

Obviously it created more unexpected behavior - it rendered “pre” and “code” tags before my questions and answers. After couple of hours I found this fix:

https://discourse.gohugo.io/t/nested-shortcodes-converting-to-pre-code-blocks/28033

Either way, thank you very much!

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