<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>template on
A Scripter's Notes</title><link>https://scripter.co/tags/template/</link><description>Recent content in template
on A Scripter's Notes</description><language>en-us</language><managingEditor>kaushal.modi@gmail.com (Kaushal Modi)</managingEditor><webMaster>kaushal.modi@gmail.com (Kaushal Modi)</webMaster><lastBuildDate>Wed, 22 Apr 2026 08:24:58 -0400</lastBuildDate><generator>Hugo -- gohugo.io</generator><docs>https://validator.w3.org/feed/docs/rss2.html</docs><atom:link href="https://scripter.co/tags/template/index.xml" rel="self" type="application/rss+xml"/><item><title>Auto-count #100DaysToOffload posts</title><link>https://scripter.co/auto-count-100daystooffload-posts/</link><description>&lt;blockquote>Let Hugo do the counting of how far along I got into the
&lt;em>#100DaysToOffload&lt;/em> challenge.&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#day-count-stub-at-the-bottom-of-each-post">&lt;em>Day count&lt;/em> stub at the bottom of each post&lt;/a>&lt;/li>
&lt;li>&lt;a href="#get-post-index-with-tag-dot-html-partial">&lt;code>get-post-index-with-tag.html&lt;/code> Partial&lt;/a>&lt;/li>
&lt;li>&lt;a href="#closing">Closing&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;p>I learned about the &lt;a href="https://100daystooffload.com/">&lt;em>#100DaysToOffload&lt;/em>&lt;/a> challenge from my &lt;a href="https://mastodon.technology/">Mastodon&lt;/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
&lt;span class="sidenote-number">&lt;small class="sidenote">
My grand total number of posts from the last two years is
zero, and this is my 5th post this year. 😆
&lt;/small>&lt;/span>
.&lt;/p>
&lt;p>&lt;a href="https://scripter.co/sidenotes-using-only-css/">Sidenotes using only CSS&lt;/a> was the first post of my first
&lt;em>#100DaysToOffload&lt;/em> challenge that I started on &lt;span class="timestamp-wrapper">&lt;span class="timestamp">&amp;lt;2022-02-03 Thu&amp;gt;&lt;/span>&lt;/span>. May
be I will get to 100 posts by &lt;span class="timestamp-wrapper">&lt;span class="timestamp">&amp;lt;2023-02-02 Thu&amp;gt; &lt;/span>&lt;/span> 🤞
.. &lt;em>Just may be&lt;/em>. But regardless, I am already enjoying writing once
again, and it&amp;rsquo;s great to see the Day count (counting up to 100)
increase with each new post!&lt;/p>
&lt;p>But it&amp;rsquo;s not fun to manually type that &lt;em>Day count&lt;/em> in each post. This
post is about a little Hugo templating code, a &lt;a href="https://gohugo.io/templates/partials/">Hugo partial&lt;/a>
&lt;span class="sidenote-number">&lt;small class="sidenote">
Think of a &lt;em>Hugo partial&lt;/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).
&lt;/small>&lt;/span>
, that does that job for me 😎.&lt;/p>
&lt;dl>
&lt;dt>Definition of this mission&lt;/dt>
&lt;dd>I want to have a line at the bottom of
all posts reading &amp;ldquo;This is Day NN of #100DaysToOffload.&amp;rdquo;. But that
should happen
&lt;mark>only for the posts with a &lt;em>100DaysToOffload&lt;/em> tag&lt;/mark> .&lt;/dd>
&lt;/dl>
&lt;h2 id="day-count-stub-at-the-bottom-of-each-post">&lt;em>Day count&lt;/em> stub at the bottom of each post&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#day-count-stub-at-the-bottom-of-each-post">#&lt;/a>&lt;/h2>
&lt;p>So I defined a &lt;em>partial&lt;/em> that would calculate that Day count, and then
I called that &lt;em>partial&lt;/em> at the bottom of my &lt;em>single&lt;/em>
&lt;span class="sidenote-number">&lt;small class="sidenote">
The &lt;a href="https://gohugo.io/templates/single-page-templates/">&lt;em>single&lt;/em>&lt;/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 &lt;em>list&lt;/em> template is
used to render &amp;ldquo;list&amp;rdquo; type pages like the &lt;a href="https://scripter.co/">home page&lt;/a>, pages listing all
the &lt;a href="https://scripter.co/posts/"> posts &lt;/a>, all the &lt;a href="https://scripter.co/tags/"> tags &lt;/a>, etc.
&lt;/small>&lt;/span>
template.&lt;/p>
&lt;p>The template file for &lt;em>single&lt;/em> pages is typically
&lt;code>layouts/_default/single.html&lt;/code> in the site or theme directory. Below
snippet is added towards the bottom of that template.&lt;/p>
&lt;p>&lt;a id="code-snippet--using-get-post-index-with-tag">&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-go-html-template" data-lang="go-html-template">&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$day&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="nx">partial&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s">&amp;#34;get-post-index-with-tag.html&amp;#34;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="nx">dict&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s">&amp;#34;page&amp;#34;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="na">.&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s">&amp;#34;tag&amp;#34;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s">&amp;#34;100DaysToOffload&amp;#34;&lt;/span>&lt;span class="o">))&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">if&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="k">gt&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$day&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">0&lt;/span>&lt;span class="o">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">printf&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s">`&amp;lt;small id=&amp;#34;day-counter&amp;#34;&amp;gt;This is &amp;lt;strong&amp;gt;Day %d&amp;lt;/strong&amp;gt; of &amp;lt;a href=&amp;#34;https://100daystooffload.com/&amp;#34;&amp;gt;#100DaysToOffload&amp;lt;/a&amp;gt;.&amp;lt;/small&amp;gt;`&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$day&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">|&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">safeHTML&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">end&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="src-block-caption">
&lt;span class="src-block-number">&lt;a href="#code-snippet--using-get-post-index-with-tag">Code Snippet 1&lt;/a>:&lt;/span>
Using the &lt;code>get-post-index-with-tag.html&lt;/code> partial
&lt;/div>
&lt;p>Explanation of the above snippet:&lt;/p>
&lt;ul>
&lt;li>&lt;em>Partial&lt;/em> &lt;code>get-post-index-with-tag.html&lt;/code> is called with a &lt;code>dict&lt;/code> type
input (think of a map or associative array) with keys &lt;code>page&lt;/code> and
&lt;code>tag&lt;/code>.&lt;/li>
&lt;li>This &lt;em>partial&lt;/em> will return a number representing the chronological
index of the current page in a pool of the specified tag.&lt;/li>
&lt;li>Almost always, you would want to pass the current page&amp;rsquo;s
context&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> &amp;ndash; the &lt;em>dot&lt;/em> &amp;ndash; to the called &lt;em>partial&lt;/em> so that it
can extract useful metadata from the page&amp;rsquo;s context, like the page
parameters, content, etc. So the &amp;ldquo;.&amp;rdquo; or the current page context is
passed to the partial using the &lt;code>page&lt;/code> key.&lt;/li>
&lt;li>The &lt;code>tag&lt;/code> key is &lt;em>&amp;ldquo;100DaysToOffload&amp;rdquo;&lt;/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.&lt;/li>
&lt;li>If the &lt;em>partial&lt;/em> returns a non-zero value,
&lt;span class="sidenote-number">&lt;small class="sidenote">
The &lt;em>partial&lt;/em> call will return a value of 0 if a page is &lt;strong>not&lt;/strong>
tagged that &lt;em>&amp;ldquo;tag&amp;rdquo;&lt;/em> key&amp;rsquo;s value.
&lt;/small>&lt;/span>
the &lt;em>Day count&lt;/em> line is printed with the shown HTML formatting.&lt;/li>
&lt;/ul>
&lt;h2 id="get-post-index-with-tag-dot-html-partial">&lt;code>get-post-index-with-tag.html&lt;/code> Partial&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#get-post-index-with-tag-dot-html-partial">#&lt;/a>&lt;/h2>
&lt;p>Below is the definition of the &lt;em>partial&lt;/em>. It is saved as
&lt;code>layouts/partials/get-post-index-with-tag.html&lt;/code> in the site or theme
directory.&lt;/p>
&lt;p>&lt;a id="code-snippet--defn-get-post-index-with-tag">&lt;/a>&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt" id="org-coderef--52750f-1">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-1"> 1&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-2">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-2"> 2&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-3">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-3"> 3&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-4">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-4"> 4&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-5">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-5"> 5&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-6">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-6"> 6&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-7">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-7"> 7&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-8">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-8"> 8&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-9">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-9"> 9&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-10">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-10">10&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-11">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-11">11&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-12">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-12">12&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-13">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-13">13&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-14">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-14">14&lt;/a>
&lt;/span>&lt;span class="lnt" id="org-coderef--52750f-15">&lt;a style="outline: none; text-decoration:none; color:inherit" href="#org-coderef--52750f-15">15&lt;/a>
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-go-html-template" data-lang="go-html-template">&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$page&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="na">.page&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$tag&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="na">.tag&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$index&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">-1&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">if&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="nx">in&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$page&lt;/span>&lt;span class="na">.Params.tags&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$tag&lt;/span>&lt;span class="o">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">with&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">site&lt;/span>&lt;span class="na">.Taxonomies.tags&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$weighted_pages&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">index&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="na">.&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="nx">$tag&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">|&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">lower&lt;/span>&lt;span class="o">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">range&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$idx&lt;/span>&lt;span class="o">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$p&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">:=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$weighted_pages&lt;/span>&lt;span class="na">.Pages.ByDate&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">if&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="k">eq&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$page&lt;/span>&lt;span class="na">.Permalink&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$p&lt;/span>&lt;span class="na">.Permalink&lt;/span>&lt;span class="o">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$index&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$idx&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">end&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">end&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">end&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">end&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$index&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="nx">add&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$index&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">1&lt;/span>&lt;span class="o">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">{{&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">return&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nx">$index&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="cp">}}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;div class="src-block-caption">
&lt;span class="src-block-number">&lt;a href="#code-snippet--defn-get-post-index-with-tag">Code Snippet 2&lt;/a>:&lt;/span>
Definition of &lt;code>get-post-index-with-tag.html&lt;/code> partial
&lt;/div>
&lt;ul>
&lt;li>
&lt;p>The partial was passed in a map with keys &amp;ldquo;page&amp;rdquo; and &amp;ldquo;tag&amp;rdquo;. We save
those to local variables &lt;code>$page&lt;/code> and &lt;code>$tag&lt;/code>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>On &lt;a href="#org-coderef--52750f-3">line 3&lt;/a>, a local variable &lt;code>$index&lt;/code> is
initialized to -1. Later in the code (line &lt;a href="#org-coderef--52750f-14">14&lt;/a>),
we increment this variable by 1 and return that. So, if &lt;code>$tag&lt;/code> is
not found for a page, &lt;code>$index&lt;/code> + 1 = 0 is returned.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;a href="#org-coderef--52750f-4">Line 4&lt;/a> checks if the current page&amp;rsquo;s &lt;em>tags&lt;/em>
front-matter has the &lt;code>$tag&lt;/code> (which is set to &lt;em>&amp;ldquo;100DaysToOffload&amp;rdquo;&lt;/em> in
&lt;a href="#code-snippet--using-get-post-index-with-tag">Code Snippet 1&lt;/a>).&lt;/p>
&lt;/li>
&lt;li>
&lt;p>In &lt;a href="#org-coderef--52750f-6">line 6&lt;/a>, we get an object
&lt;code>$weighted_pages&lt;/code> that has a collection or list of all pages with
the &lt;code>$tag&lt;/code> tag set. &lt;em>That list will contain the current page too.&lt;/em>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>In &lt;a href="#org-coderef--52750f-7">line 7&lt;/a>, we sort this filtered
page list &lt;code>$weighted_pages.Pages&lt;/code> by &lt;em>date&lt;/em> using the &lt;code>.ByDate&lt;/code>
method, and then loop through that.
&lt;span class="sidenote-number">&lt;small class="sidenote">
Here, we just get a list of &lt;strong>all&lt;/strong> the posts with that &lt;code>$tag&lt;/code>. We
don&amp;rsquo;t deal with cases like.. &amp;ldquo;What if I were on my second or later
attempts of &lt;em>#100DaysToOffload&lt;/em> challenge?&amp;rdquo;. I will deal with that
when I need to, and then probably I will have a followup post for
that 😄.
&lt;/small>&lt;/span>
In this line, &lt;code>$idx&lt;/code> is the loop counter variable, and &lt;code>$p&lt;/code> will
hold the page item from the list.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>In &lt;a href="#org-coderef--52750f-8">line 8&lt;/a>, the permalink of each page
(&lt;code>$p&lt;/code>) in that page list is compared with the current page&amp;rsquo;s
permalink. When we find a match, we update the &lt;code>$index&lt;/code> variable
with that loop counter variable&amp;rsquo;s value. This match must happen only
once because all pages have unique permalinks.
&lt;span class="sidenote-number">&lt;small class="sidenote">
I would have liked to use a &lt;code>break&lt;/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&amp;rsquo;t have that feature.
&lt;/small>&lt;/span>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>The list indices begin with 0. So if this were the first page in the
date sorted list, &lt;code>$index&lt;/code> would be set to 0. But &amp;ldquo;Day 0 of 100&amp;rdquo;
would sound weird. So in &lt;a href="#org-coderef--52750f-14">line 14&lt;/a>, we first
increment the &lt;code>$index&lt;/code> value, and then return that.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="closing">Closing&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#closing">#&lt;/a>&lt;/h2>
&lt;p>That&amp;rsquo;s it!&lt;/p>
&lt;p>With that little &lt;em>partial&lt;/em> defined and then called in the
&lt;em>single.html&lt;/em> layout file, you can see how the automatic Day count
shows up &lt;a href="#day-counter"> below&lt;/a>.&lt;/p>
&lt;p>Happy Hugo Templating! 🎉&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>You can learn more about Hugo&amp;rsquo;s &lt;em>dot&lt;/em> notion and page context
from &lt;a href="https://www.regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/">this wonderfully written post&lt;/a> by Regis Philibert.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/hugo">hugo</category><category domain="https://scripter.co/tags/100daystooffload">100DaysToOffload</category><category domain="https://scripter.co/tags/template">template</category><category domain="https://scripter.co/tags/partial">partial</category><guid>https://scripter.co/auto-count-100daystooffload-posts/</guid><pubDate>Sun, 20 Feb 2022 02:39:00 -0500</pubDate></item></channel></rss>