Custom pipes

Only feature that i miss is to be able to write own pieces of code (like partials) that would return value.
It would get variable (slice, map, string, integer, bool, …) and return something.

Now i have too heavy layouts (20 lines of code) where i’m using ranges and appends to build one single variable which is than used to output 4 lines of html. In other template I have same 20 lines of code to produce very same variable which is than used to output different html.

So i’m repeating myself over and over in many layouts to achieve the same.

Desired solution:
{{ $description := .Page | customdescription }}

Here is real working example:
{{ $description := slice -}}
{{- if .Description -}}
{{- $description = $description | append (.Description | markdownify | plainify) -}}
{{- else if .Content -}}
{{- $description = $description | append (.Content | plainify) -}}
{{- else if .Params.blocks -}}
{{- range .Params.blocks -}}
{{- range $key,$item := . -}}
{{- if or (eq $key “text”) (eq $key “title”) -}}
{{- $description = $description | append ($item | markdownify | plainify) -}}
{{- end -}}

      {{- if (eq $key "items") -}}
        {{- range $item -}}
          {{- range $key2,$item2 := . -}}
            {{- if or (eq $key2 "text") (eq $key2 "title") -}}
              {{- $description = $description | append ($item2 | markdownify | plainify) -}}
            {{- end -}}
          {{- end -}}
        {{- end -}}
      {{- end -}}

    {{- end -}}
  {{- end -}}
{{- end -}}
{{- $description = (replaceRE "\\n+|\\&nbsp\\;" " " (delimit $description "; ")) | default .Site.Params.description | truncate 200 -}}

Have you tried passing .Page to your partial call?

{{ $description := partial "customdescription.html" .Page }}
2 Likes

Great. It just made my day. This is what I needed.

1 Like

Also remember that in Hugo 0.55 you can use the new return keyword in partials to return any type. That may not be updated in the docs, but I’ll get there eventually.

3 Likes