Logic going away in a puff of smoke?

       {{ $bla := "-/-" }}
      {{ if .Page.IsPage }}
        {{ $bla := "page" }}
      {{ else }}
        {{ $bla := "branch" }}
      {{ end }}

      Content is {{ printf "%s" $bla }}

Assume I have the above in a layout.

Guess my surprise when the layout spit out “-/-” rather then page or branch.

In those hundred programming languages I already forgot about in my life, I always thought there is if and else and one of them will be executed. Today I learned there is alternatives. Just do nothing. Is this template language a government project?

Or might I have missed an important aspect?


{{ $bla = “page” }}

Hmmm! Ok! I tried ? :wink:

          {{ $bla = "-/-" }}
          {{ if .Page.IsPage }}
            {{ $bla = "page" }}
          {{ else }}
            {{ $bla = "branch" }}
          {{ end }}

Rebuild failed:

Failed to render pages: render of "home" failed: "/Users/prefect/hugo/ik-webpage/themes/first/layouts/_default/baseof.html:7:20": execute of template failed: template: _default/index.html:7:20: executing "_default/index.html" at <"-/-">: undefined variable: $bla

Does not seem to be my day.

OMG , now I see it:

$bla := "string"

declares the variable and

$bla = "otherstring"

modifies it.

Learned + 1



Yea, I should have written a little longer explanation … The Go template syntax is influenced by Go’s syntax in this area.


Actually I found that already very early. Regrettably the documentation of hugo’s specifics on the templating is as “sparse” as the original go template doc: a lot of specific howto, very little to understand the concept to draw own conclusions. So the learning curve is first analyse and reverse-engineer the thoughts of the create before one can really use it full-scale. Is a bit unlucky.

1 Like