Conditional render-heading.html (can't evaluate field Params in type goldmark.headingContext)

I got following render-heading.html file in layout\_default\_markup

<h{{ .Level }}
{{- range $k, $v := .Attributes -}}{{- printf " %s=%q" $k $v | safeHTMLAttr -}}{{- end -}}
id="{{ .Anchor | safeURL }}">{{ .Text | safeHTML }}
<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
</h{{ .Level }}>

I would like to add condition to it, like {{ if .Params.faq }} it will not display <a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>.

Like:

<h{{ .Level }}
{{- range $k, $v := .Attributes -}}{{- printf " %s=%q" $k $v | safeHTMLAttr -}}{{- end -}}
id="{{ .Anchor | safeURL }}">{{ .Text | safeHTML }}
{{ if (not .Params.faq) }}<a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>{{ end }}
</h{{ .Level }}>

but that result in an error

execute of template failed: template: _default/_markup/render-heading.html:1:176: executing "_default/_markup/render-heading.html" at <.Params.faq>: can't evaluate field Params in type goldmark.headingContext

Is there any other way to achieve that?

Context…

{{ if (not .Page.Params.faq) }}

Sorry, yes.

I got a page template.
By default its an article unless got specified in frontmatter.

---
faq: true
---

In articles, I want headings (h2, h3 etc) with anchor links, but on the FAQ page any headings there to be without an anchor links.

ps. if not possible that not a problem, as got alternative approach to use. Just want to double check if render-heading can be conditional based on frontmatter parameters?

Yes.

From within the render hook, check .Page.Params.faq, not .Params.faq.

1 Like

Thank you.

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