Here is a technique that I use to split a string and output a specified number of words.
Full code:
{{- $caption := $.Site.Params.caption -}}
{{- $caption = split .node.text " " -}}
{{- range first 22 $caption -}}{{- . }} {{ end -}}
{{- range first 1 (after 22 $caption) -}}{{- . -}}{{- end -}}…{{- end -}}
The above can be adapted anyway one needs also it can be used to generated custom post summaries provided that the post content is sanitized beforehand with the plainify
function to remove any HTML.
Step-by-step explanation
The text in this example is provided by a .Params.caption
in the project’s config.
{{ $caption := $.Site.Params.caption }}
{{ $caption = split .node.text " " }}
Use the split function to convert the string into substrings, (the delimiter is the space between the words i.e. " "
).
{{ range first 22 $caption }}
range
through the substrings and enter the desired number of words.
{{ . }} {{ end }}
Note the space between {{ . }}
and {{ end }}
, it is required because otherwise the words will be printed consecutively.
{{ range first 1 (after 22 $caption) }}
{{ . }}{{ end }}…{{ end }}
For the last word I do not need a following space but an ellipsis.
That’s all.