Cannot add comments at beginning of the define block partial

If you structure your layouts/_default/ like this:

baseof.html:

<!-- head, body balabala essential elements -->
<main>
{{- block "main" . }}{{ end -}}
</main>

list.html/single.html:

<!-- I want to add a license here at the beginning of the file -->
{{- define "main" }}
<!-- your code here-->
{{- end }}

Then, you probably can not find the _index.md in your content. If you have layouts/index.html like the list.html/single.html before, you will get an error as below:

console.warn('"head" or "body" tag is required in html to append livereload script. As a fallback, Hugo injects it somewhere but it might not work properly.');

So, how to add my license at the beginning of my block define partial.

Add the license INSIDE after {{ define "main" }}.

There is no doubt that this is one solution. However, this makes it look very dirty.

Is there a better solution? Or mark it as a bug or feature?

You are circumventing the way Hugo performs. But perhaps someone more experienced will give you a better answer.

Use template comments instead of HTML comments.

{{/* This one line comment will not appear on the rendered page. */}}

{{/*
Neither will
this block
comment.
*/}}

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  {{ end }}
{{ end }}

You can only place newlines and template comments before and/or after the block definition.

This is a Go templating requirement.

Thanks a lot! This is what I want. )

I thought you wanted the license to appear on the website! My bad! Anyways, it’s as simple as what Joe shared.

Haha. Whatever, thanks for your kind help. )

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