<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.101.0">Hugo</generator><title type="html">template on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/tags/template/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/tags/template/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/tags/template/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/tags/template/jf2feed.json" rel="alternate" type="application/jf2feed+json" title="jf2feed"/><updated>2026-04-22T08:24:58-04:00</updated><author><name>Kaushal Modi</name><email>kaushal.modi@gmail.com</email></author><id>https://scripter.co/tags/template/</id><entry><title type="html">Auto-count #100DaysToOffload posts</title><link href="https://scripter.co/auto-count-100daystooffload-posts/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://scripter.co/grep-po/?utm_source=atom_feed" rel="related" type="text/html" title="grep -Po"/><link href="https://scripter.co/generics-not-exactly-in-systemverilog/?utm_source=atom_feed" rel="related" type="text/html" title="Generics (not exactly) in SystemVerilog"/><link href="https://scripter.co/sidenotes-using-ox-hugo/?utm_source=atom_feed" rel="related" type="text/html" title="Sidenotes using ox-hugo"/><link href="https://scripter.co/sidenotes-using-only-css/?utm_source=atom_feed" rel="related" type="text/html" title="Sidenotes using only CSS"/><id>https://scripter.co/auto-count-100daystooffload-posts/</id><author><name>Kaushal Modi</name></author><published>2022-02-20T02:39:00-05:00</published><updated>2022-02-20T02:39:00-05:00</updated><content type="html"><![CDATA[<blockquote>Let Hugo do the counting of how far along I got into the
<em>#100DaysToOffload</em> challenge.</blockquote><div class="ox-hugo-toc toc">
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#day-count-stub-at-the-bottom-of-each-post"><em>Day count</em> stub at the bottom of each post</a></li>
<li><a href="#get-post-index-with-tag-dot-html-partial"><code>get-post-index-with-tag.html</code> Partial</a></li>
<li><a href="#closing">Closing</a></li>
</ul>
</div>
<!--endtoc-->
<p>I learned about the <a href="https://100daystooffload.com/"><em>#100DaysToOffload</em></a> challenge from my <a href="https://mastodon.technology/">Mastodon</a>
stream about a month back. Motivated by that, this year I have now
already written a lot more blog posts than my last two years combined
<span class="sidenote-number"><small class="sidenote">
My grand total number of posts from the last two years is
zero, and this is my 5th post this year. 😆
</small></span>
.</p>
<p><a href="/sidenotes-using-only-css/">Sidenotes using only CSS</a> was the first post of my first
<em>#100DaysToOffload</em> challenge that I started on <span class="timestamp-wrapper"><span class="timestamp">&lt;2022-02-03 Thu&gt;</span></span>. May
be I will get to 100 posts by <span class="timestamp-wrapper"><span class="timestamp">&lt;2023-02-02 Thu&gt; </span></span> 🤞
.. <em>Just may be</em>. But regardless, I am already enjoying writing once
again, and it&rsquo;s great to see the Day count (counting up to 100)
increase with each new post!</p>
<p>But it&rsquo;s not fun to manually type that <em>Day count</em> in each post. This
post is about a little Hugo templating code, a <a href="https://gohugo.io/templates/partials/">Hugo partial</a>
<span class="sidenote-number"><small class="sidenote">
Think of a <em>Hugo partial</em> as a function which you can then call
anywhere in your template or theme files (not in the content files
like Org or Markdown).
</small></span>
, that does that job for me 😎.</p>
<dl>
<dt>Definition of this mission</dt>
<dd>I want to have a line at the bottom of
all posts reading &ldquo;This is Day NN of #100DaysToOffload.&rdquo;. But that
should happen
<mark>only for the posts with a <em>100DaysToOffload</em> tag</mark> .</dd>
</dl>

<h2 id="day-count-stub-at-the-bottom-of-each-post"><em>Day count</em> stub at the bottom of each post&nbsp;<a class="headline-hash no-text-decoration" href="#day-count-stub-at-the-bottom-of-each-post">#</a></h2>


<p>So I defined a <em>partial</em> that would calculate that Day count, and then
I called that <em>partial</em> at the bottom of my <em>single</em>
<span class="sidenote-number"><small class="sidenote">
The <a href="https://gohugo.io/templates/single-page-templates/"><em>single</em></a> template is the template that will be used to render the
content from a single content file, like a post page.. like the page
you are seeing right now. Compared to that, the <em>list</em> template is
used to render &ldquo;list&rdquo; type pages like the <a href="/">home page</a>, pages listing all
the <a href="/posts/"> posts </a>, all the <a href="/tags/"> tags </a>, etc.
</small></span>
template.</p>
<p>The template file for <em>single</em> pages is typically
<code>layouts/_default/single.html</code> in the site or theme directory. Below
snippet is added towards the bottom of that template.</p>
<p><a id="code-snippet--using-get-post-index-with-tag"></a></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go-html-template" data-lang="go-html-template"><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="nx">$day</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">(</span><span class="nx">partial</span><span class="w"> </span><span class="s">&#34;get-post-index-with-tag.html&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">                    </span><span class="o">(</span><span class="nx">dict</span><span class="w"> </span><span class="s">&#34;page&#34;</span><span class="w"> </span><span class="na">.</span><span class="w"> </span><span class="s">&#34;tag&#34;</span><span class="w"> </span><span class="s">&#34;100DaysToOffload&#34;</span><span class="o">))</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">(</span><span class="k">gt</span><span class="w"> </span><span class="nx">$day</span><span class="w"> </span><span class="nx">0</span><span class="o">)</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">    <span class="cp">{{</span><span class="w"> </span><span class="k">printf</span><span class="w"> </span><span class="s">`&lt;small id=&#34;day-counter&#34;&gt;This is &lt;strong&gt;Day %d&lt;/strong&gt; of &lt;a href=&#34;https://100daystooffload.com/&#34;&gt;#100DaysToOffload&lt;/a&gt;.&lt;/small&gt;`</span><span class="w"> </span><span class="nx">$day</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nx">safeHTML</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="cp">}}</span>
</span></span></code></pre></div><div class="src-block-caption">
  <span class="src-block-number"><a href="#code-snippet--using-get-post-index-with-tag">Code Snippet 1</a>:</span>
  Using the <code>get-post-index-with-tag.html</code> partial
</div>
<p>Explanation of the above snippet:</p>
<ul>
<li><em>Partial</em> <code>get-post-index-with-tag.html</code> is called with a <code>dict</code> type
input (think of a map or associative array) with keys <code>page</code> and
<code>tag</code>.</li>
<li>This <em>partial</em> will return a number representing the chronological
index of the current page in a pool of the specified tag.</li>
<li>Almost always, you would want to pass the current page&rsquo;s
context<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup> &ndash; the <em>dot</em> &ndash; to the called <em>partial</em> so that it
can extract useful metadata from the page&rsquo;s context, like the page
parameters, content, etc. So the &ldquo;.&rdquo; or the current page context is
passed to the partial using the <code>page</code> key.</li>
<li>The <code>tag</code> key is <em>&ldquo;100DaysToOffload&rdquo;</em> as we want to know which
number post is the current post with that specific tag. For example,
this is the 5th post so far.</li>
<li>If the <em>partial</em> returns a non-zero value,
<span class="sidenote-number"><small class="sidenote">
The <em>partial</em> call will return a value of 0 if a page is <strong>not</strong>
tagged that <em>&ldquo;tag&rdquo;</em> key&rsquo;s value.
</small></span>
the <em>Day count</em> line is printed with the shown HTML formatting.</li>
</ul>

<h2 id="get-post-index-with-tag-dot-html-partial"><code>get-post-index-with-tag.html</code> Partial&nbsp;<a class="headline-hash no-text-decoration" href="#get-post-index-with-tag-dot-html-partial">#</a></h2>


<p>Below is the definition of the <em>partial</em>. It is saved as
<code>layouts/partials/get-post-index-with-tag.html</code> in the site or theme
directory.</p>
<p><a id="code-snippet--defn-get-post-index-with-tag"></a></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt" id="org-coderef--52750f-1"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-1"> 1</a>
</span><span class="lnt" id="org-coderef--52750f-2"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-2"> 2</a>
</span><span class="lnt" id="org-coderef--52750f-3"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-3"> 3</a>
</span><span class="lnt" id="org-coderef--52750f-4"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-4"> 4</a>
</span><span class="lnt" id="org-coderef--52750f-5"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-5"> 5</a>
</span><span class="lnt" id="org-coderef--52750f-6"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-6"> 6</a>
</span><span class="lnt" id="org-coderef--52750f-7"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-7"> 7</a>
</span><span class="lnt" id="org-coderef--52750f-8"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-8"> 8</a>
</span><span class="lnt" id="org-coderef--52750f-9"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-9"> 9</a>
</span><span class="lnt" id="org-coderef--52750f-10"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-10">10</a>
</span><span class="lnt" id="org-coderef--52750f-11"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-11">11</a>
</span><span class="lnt" id="org-coderef--52750f-12"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-12">12</a>
</span><span class="lnt" id="org-coderef--52750f-13"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-13">13</a>
</span><span class="lnt" id="org-coderef--52750f-14"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-14">14</a>
</span><span class="lnt" id="org-coderef--52750f-15"><a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-15">15</a>
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-go-html-template" data-lang="go-html-template"><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="nx">$page</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="na">.page</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="nx">$tag</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="na">.tag</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="nx">$index</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">-1</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">(</span><span class="nx">in</span><span class="w"> </span><span class="nx">$page</span><span class="na">.Params.tags</span><span class="w"> </span><span class="nx">$tag</span><span class="o">)</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">    <span class="cp">{{</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="nx">site</span><span class="na">.Taxonomies.tags</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">        <span class="cp">{{</span><span class="w"> </span><span class="nx">$weighted_pages</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">index</span><span class="w"> </span><span class="na">.</span><span class="w"> </span><span class="o">(</span><span class="nx">$tag</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nx">lower</span><span class="o">)</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">        <span class="cp">{{</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">$idx</span><span class="o">,</span><span class="w"> </span><span class="nx">$p</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">$weighted_pages</span><span class="na">.Pages.ByDate</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">            <span class="cp">{{</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">(</span><span class="k">eq</span><span class="w"> </span><span class="nx">$page</span><span class="na">.Permalink</span><span class="w"> </span><span class="nx">$p</span><span class="na">.Permalink</span><span class="o">)</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">                <span class="cp">{{</span><span class="w"> </span><span class="nx">$index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">$idx</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">            <span class="cp">{{</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">        <span class="cp">{{</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl">    <span class="cp">{{</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="nx">$index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">(</span><span class="nx">add</span><span class="w"> </span><span class="nx">$index</span><span class="w"> </span><span class="nx">1</span><span class="o">)</span><span class="w"> </span><span class="cp">}}</span>
</span></span><span class="line"><span class="cl"><span class="cp">{{</span><span class="w"> </span><span class="nx">return</span><span class="w"> </span><span class="nx">$index</span><span class="w"> </span><span class="cp">}}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="src-block-caption">
  <span class="src-block-number"><a href="#code-snippet--defn-get-post-index-with-tag">Code Snippet 2</a>:</span>
  Definition of <code>get-post-index-with-tag.html</code> partial
</div>
<ul>
<li>
<p>The partial was passed in a map with keys &ldquo;page&rdquo; and &ldquo;tag&rdquo;. We save
those to local variables <code>$page</code> and <code>$tag</code>.</p>
</li>
<li>
<p>On <a href="#org-coderef--52750f-3">line 3</a>, a local variable <code>$index</code> is
initialized to -1. Later in the code (line <a href="#org-coderef--52750f-14">14</a>),
we increment this variable by 1 and return that. So, if <code>$tag</code> is
not found for a page, <code>$index</code> + 1 = 0 is returned.</p>
</li>
<li>
<p><a href="#org-coderef--52750f-4">Line 4</a> checks if the current page&rsquo;s <em>tags</em>
front-matter has the <code>$tag</code> (which is set to <em>&ldquo;100DaysToOffload&rdquo;</em> in
<a href="#code-snippet--using-get-post-index-with-tag">Code Snippet 1</a>).</p>
</li>
<li>
<p>In <a href="#org-coderef--52750f-6">line 6</a>, we get an object
<code>$weighted_pages</code> that has a collection or list of all pages with
the <code>$tag</code> tag set. <em>That list will contain the current page too.</em></p>
</li>
<li>
<p>In <a href="#org-coderef--52750f-7">line 7</a>, we sort this filtered
page list <code>$weighted_pages.Pages</code> by <em>date</em> using the <code>.ByDate</code>
method, and then loop through that.
<span class="sidenote-number"><small class="sidenote">
Here, we just get a list of <strong>all</strong> the posts with that <code>$tag</code>. We
don&rsquo;t deal with cases like.. &ldquo;What if I were on my second or later
attempts of <em>#100DaysToOffload</em> challenge?&rdquo;. I will deal with that
when I need to, and then probably I will have a followup post for
that 😄.
</small></span>
In this line, <code>$idx</code> is the loop counter variable, and <code>$p</code> will
hold the page item from the list.</p>
</li>
<li>
<p>In <a href="#org-coderef--52750f-8">line 8</a>, the permalink of each page
(<code>$p</code>) in that page list is compared with the current page&rsquo;s
permalink. When we find a match, we update the <code>$index</code> variable
with that loop counter variable&rsquo;s value. This match must happen only
once because all pages have unique permalinks.
<span class="sidenote-number"><small class="sidenote">
I would have liked to use a <code>break</code> statement once the first match
was found, to avoid wasting time going through other pages in the
list. But the Hugo/Go templating doesn&rsquo;t have that feature.
</small></span></p>
</li>
<li>
<p>The list indices begin with 0. So if this were the first page in the
date sorted list, <code>$index</code> would be set to 0. But &ldquo;Day 0 of 100&rdquo;
would sound weird. So in <a href="#org-coderef--52750f-14">line 14</a>, we first
increment the <code>$index</code> value, and then return that.</p>
</li>
</ul>

<h2 id="closing">Closing&nbsp;<a class="headline-hash no-text-decoration" href="#closing">#</a></h2>


<p>That&rsquo;s it!</p>
<p>With that little <em>partial</em> defined and then called in the
<em>single.html</em> layout file, you can see how the automatic Day count
shows up <a href="#day-counter"> below</a>.</p>
<p>Happy Hugo Templating! 🎉</p>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>You can learn more about Hugo&rsquo;s <em>dot</em> notion and page context
from <a href="https://www.regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/">this wonderfully written post</a> by Regis Philibert.&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content><category scheme="https://scripter.co/categories/hugo" term="hugo" label="hugo"/><category scheme="https://scripter.co/tags/100daystooffload" term="100daystooffload" label="100DaysToOffload"/><category scheme="https://scripter.co/tags/template" term="template" label="template"/><category scheme="https://scripter.co/tags/partial" term="partial" label="partial"/></entry></feed>