Go Template comments

Continuing the discussion from Content directories per environment:

I knew that code in <!-- {{ some Go tag }} --> got evaluated. But not sure about this one.


Update

On brief testing using:

{{/* {{ $foo := 1 }} */}}

{{ $foo }}

I get an error:

… undefined variable “$foo”

So looks like stuff inside {{/* .. */}} is not evaluated.


But there is no error if I do:

<!-- {{ $foo := 1 }} -->

{{ $foo }}

Related PR by @cmal :

Related discussion that triggered that PR:

I would expect that error to come up, because the way I would “interpret” loops (or "everything that is between opening and closing tag of something) is that they are “enclosed” in themself… I mean the comment around the {{ $foo := 1 }} leads to $foo being discarded. But inside of the comment $foo might have “lived”. One of my comments was for instance:

{{/* {{ partial "footer/author.html" . }} */}}

What if the partial was loaded, executed (think pager or something else sinister here) and then discarded - all within the comment.

It was just a guess though… I don’t know how Go is doing templates. But the way I understand shortcodes for instance is that a shortcode inside of a shortcode gets rendered. so if that get’s parsed inside of content, then rendered, then Go “sees” the comment signs around it and discards it…

But somehow I think that would have come up elsewhere already and documented or fixed.

That’s a valid point, but even this works without errors:

{{/* {{ laksdjf }} */}}

whereas:

{{ laksdjf }}

throws this error as expected:

function “laksdjf” not defined

Yes, but it could be that the content is rendered but then both content and errors discarded. However, I just verified that and it’s not the case: nothing is evaluated within go template comments.

    {{/*{{ range (seq 1 100) }}
        {{ range (seq 1 100) }}
            {{ add 0 1 }}
        {{ end }}
    {{ end }}*/}}

This snippet add 2 seconds to my generation time when I remove the comment blocks. But when it’s commented out, I’m back to 200ms :slight_smile:

2 Likes

Good. How about the timing of both version? Maybe parsing of of the code to find comments to strip them out is expensive - but takes care of “commented” errors. I took out most of my comments now and am at around 10 seconds per rebuild for 2000 posts. Compared to 30 to 60 seconds before it’s a huge win.

There are lots of changes in my code between yesterday (60s) and today (10s) but they can be sorted into:

  • removing config variables that have the defautl value in config.toml
  • removing many partials and putting them directly into their parents (like single.html, home.html etc.) and
  • removing comments

something there must have made hugo happier :slight_smile:

maybe it’s a processor thing with my computer? I see CPU maxing out while recompiling the site…

slow: https://bitbucket.org/pkollitsch/samui-samui.de/commits/bd3ed752b2a821698df133bd981b5234229550c4
fast: https://bitbucket.org/pkollitsch/samui-samui.de/commits/fcd9f28f0905eaaf77350cea0d011b8e4f0129af

I actually downloaded and generated your site. This commit brought your generation time down. That’s because you changed the number of article per page (pagination settings). More will make it faster to generate the site, but slower to load for people visiting it (because images and such).

With paginate = 5 like before, it takes about 30s to generate the site. With #paginate (commented) it takes about 12s. With paginate = 40 it takes about 5s. In this last case the two templates that take the most time are post/single.html and partials/head.html, and both of them need to be generated for every page so that’s pretty normal.

So in the end it seems to me your performance issue/improvement has nothing to do with comments at all, or even with how many partials you’re using. Can you confirm you get similar results on your side?