Struggling to use .PlainWords in a partial

I’m trying to improve our site’s social media preview tiles, by including the first 20 words of a page’s content in the <meta description> tags if the page doesn’t have a description or summary set in the front matter. I thought this would be really simple, but I’m not able to get access to the content in the way I’d expect. I’ve tried several variants now, but none of them have worked, so I am here.

First, here is the block I THINK should work:

{{ with .Description }}
  {{ . }}
{{ else }}
  {{ with .Summary }}
    {{ . }}
  {{ else }}
    {{ .PlainWords | truncate 20 }}
  {{ end }}
{{ end }}

However, .PlainWords is always empty. I think this is a scoping problem with the nested with statements, but I’m not sure how to solve it. I’ve tried .Params.PlainWords too. Actually if I just dump out .Params there, it doesn’t have .PlainWords in it, but only the title and a few other bits of metadata.

I’m including this in a partial, but we pass the partial the . variable, so I believe that means it should have access to all the page-level variables.

Check your context.

Hi Regis, I read that article and tried to understand it. When I am including the partial, it looks like:

{{ partial "head.html" . }}

Though I also tried like this:

{{ partial "head.html" (dict "Page" .) }}

From within the partial, if I dump out the following variables (with the dict version above), they look like this:

. looks like map[Page:Page(&#34;Contributing to the Kubernetes Docs&#34;)]
$ looks like map[Page:Page(&#34;Contributing to the Kubernetes Docs&#34;)]
.Page looks like Page(&#34;Contributing to the Kubernetes Docs&#34;)

So yeah, I don’t understand the context. The weird thing is elsewhere in the partial I can get access to .Title, .Content, and all the rest.

Your partial is fine, your context problem is with your with. :smiley:
{{ with .Description }} >>> From now on, your context is the value of .Description, most likely a string, yet from within this with you’re testing .Summary… You are actually testing .Description.Summary… Same thing happens with .PlayWords.

How about…

{{ if .Params.description }}
  {{ .Params.descriptoin }}
{{ elseif .Params.summary }}
{{ .Prams.summary }}
{{ else }}
    {{ .PlainWords | truncate 20 }}
{{ end }}

Not on a computer where I can test, but assuming you’ve already read the docs on .Summary as well, since I’m writing this thinking that you mean summary as a defined param and not the auto or manually defined summaries provided out of the box with Hugo. HTH.