Detect the kind of summary (automatic or manual)

Hello, I am using {{ .Summary }} to show just a part of the content… but my code define some ellipsis “…” after it. It looks like this:

<div class="summary">{{ .Summary }}…</div>

The problem is that in some cases I’m using to define my own summary, and in some others not. And I don’t want the “…” to appear when I’ve defined the summary myself.

Is there any way to know the difference to show the “…” just when the summary is not user-defined?

Use a conditional

Have a look at with in particular.

Thanks. While I understand the logic and the structure, I fail to see how to get if the summary has been automatically done, or by me using <!--more-->

.Summary is the variable that holds both the automatic 70 words and the manual method with <!--more-->

The manual method overrides the automatic word limit.

For the purposes of explaining to you how to go about it I will assume that you want to use the manual summary for content that is longer than the 70 words of the automatic summary.

You can replace what you have with:

<div class="summary">{{ .Summary }}{{ $count := .Summary | countwords }}{{ if eq $count 70 }}...{{ end }}</div>

The above does the following:

  1. Store the summary word count in a variable with {{ $count := .Summary | countwords }}
  2. Then for up to 70 words show the ellipsis {{ if eq $count 70 }}...{{ end }}
  3. Longer summaries set by <!--more--> will not have the ellipsis.

Feel free to customize this code snippet to suit your needs.
And have a look at Hugo’s functions you will be surprised with how much you can achieve.

EDIT
Now that I think of it you can also insert the ellipsis with CSS to the summary.

Simply adjust the snippet to:

{{ $count := .Summary | countwords }}
<div class="summary{{ if eq $count 70 }} auto{{ end }}">{{ .Summary }}</div>

And then just use the CSS pseudo-selector .auto:after to insert the ellipsis. Also now you can style it any way you want.

Here is an alternative suggested by Bep. It uses .Contains to test the presence of the more tag: Detect if a more tag is in the markdown

Thanks! I think this is better solution and closer to what I actually need.