Checking if subtemplate value is defined or not

In my main layout file I’d like to do something like this for the HTML title tag:

        {{if isset "title" }}
          <title>{{template "title"}} - {{ .SiteTitle }}</title>
          <title>{{ .SiteTitle }}</title>

But the isset portion doesn’t seem to be a part of the base HTML Template package (I heard about it via a page from the Hugo website so I figure it’s a special addition they’ve added to their templating language).

I’ve also tried using “eq” instead of isset, but it didn’t appear to have the behavior I was wanting either.

Basically, in my sublayout file I want to be able to define a title (or not) that gets tacked onto the front of the Site Title automatically. If the title is not defined, then it should just display the main Site Title.

I felt it would be a bit easier, but I haven’t seen a good example of this yet so far and the documentation doesn’t seem to cover the syntax in depth for me to try and figure out on my own.

Any tips or links to additional resources is much appreciated!

Thank you!

I am not very familiar with templates, so apologies if I only add noise to this thread.

“title” seems to be a subtemplate. Since you want to test for existence of this subtemplate, I assume there is some conditional construct in the main template that defines “title” only if some condition is true, like:

{{ if <some_condition> }}{{ define "title" }}MyTitleTemplate{{ end }}

If my assumption is right so far, you could eliminate the if isset approach (which, btw is indeed a Hugo extension) by changing the above condition to:

{{ if <some_condition> }}{{ define "title" }}MyTitleTemplate{{ end }}{{ else }}{{ define "title" }}{{ .SiteTitle }}{{ end }}{{ end }}

If your “title” template is rather simple you could perhaps drop the subtemplating approach completely and just do something like

{{ if <some_condition> }}MyTitle{{ else }}{{ .SiteTitle }}{{ end }}

Hi @christophberger ,

I haven’t came back to revisit this again since the weekend, but it’s still something that seems like it will be difficult to solve using the basic built-in options within Go with the other information I’ve continued to read since I ran into the issue that day.

“title” is indeed defined in a subtemplate at the moment, but the main issue I’m noticing is that if it isn’t defined in the subtemplate, then even doing the simple:

{{ if "title" }} 

check fails with an error:

html/template:mincss.html:11:28: no such template "title"

In this case the condition I want to check is simply the existing of the subtemplate being defined.

As I’ve continued to think about this since the weekend, I did realize that the value can be set ahead of time (from within the Go code and included as part of the data variable passed into the template), but this would differ somewhat from the basic approach that was being used in the book I was using at the time to guide me on the template basics. However, this may be the approach that Go requires.

So here’s another version of the code that doesn’t result in any errors (but still doesn’t behave correctly):

        {{if eq "title" "" }}
          <title>{{template "title"}} - {{ .SiteTitle }}</title>
          <title>{{ .SiteTitle }}</title>

With the subtemplate being defined like so (empty):

{{define "title"}}{{end}}

The behavior seems to be correct, I just get the SiteTitle value displayed.

However, if I go back to the subtemplate and put in an actual value:

{{define "title"}}Client Info{{end}}

I still get just the SiteTitle value displayed (the value defined in the subtemplate doesn’t work).

This makes me feel that the comparison operators for the templates may only work correctly for dot values passed in as data to the template?

As I mentioned above, there’s ways to work around this issue by specifying these values ahead of time in the Go code and passing them in as data directly into the template, but it is a little disappointing that this other approach doesn’t also work.

Seems so. According to the text/template document, templates are not arguments.

What does the existence of the subtemplate depend on? Is there some step earlier in your process that decides whether or not to create the subtemplate? If so, could that step generate a default template like “{{ .SiteTitle }}”, rather than generating no template at all?
This way you could skip the logic in the main template entirely.

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