Why Extra newlines added after render image hook?

there are the contents in render-image.html

{{ if site.Params.replacefunc.enable }}
    {{ if hugo.IsServer }}
<p class="md__image">
    <img src='{{ .Destination | safeURL }}' alt="{{ .Text }}" {{ with .Title }} title="{{ . }}" {{ end }} />
</p>
    {{ else }}
        {{ $image_ext := replaceRE ".*\\.([^.]+)$" "$1" .Destination }}
        {{ if in site.Params.replacefunc.convertTypeList (lower $image_ext) }}
            {{ $img_abs_src := "" }}

			{{ some logic code }}
			{{ some logic code }}
			{{ some logic code }}
			{{ some logic code }}
			{{ some logic code }}

<p class="md__image">
    <img src='{{ $img_abs_src| safeURL }}' alt="{{ .Text }}" {{ with .Title }} title="{{ . }}" {{ end }} />
</p>
        {{ else }}
<p class="md__image">
    <img src='{{ .Destination | safeURL }}' alt="{{ .Text }}" {{ with .Title }} title="{{ . }}" {{ end }} />
</p>
        {{ end }}
    {{ end }}
{{ else }}
<p class="md__image">
    <img src='{{ .Destination | safeURL }}' alt="{{ .Text }}" {{ with .Title }} title="{{ . }}" {{ end }} />
</p>
{{ end }}


hugo version : 0.131.0

So, we (Hugo) isn’t adding any extra newline, but whitespace and templates can be tricky, especially considering that many editors/formatters add trailing newlines…

Some tricks:

  • End file with empty comment: {{/* "" */}}
  • A general whitespace template trick is to use the {{- and -}} delimiters to chomp whitespace.
1 Like

i tried different editors like notepad(windows build in)、notepad++、vscode。
and use chrome to chose “View page source code” (CTRL+U)

all of them show those newlines

I suggest you look at some examples to understand how to remove white space:
https://gohugo.io/render-hooks/images/#examples

And the relevant documentation:
https://gohugo.io/templates/introduction/#whitespace

1 Like

@bep {{- and -}} this works! thanks

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