Insert CTA within the list of blog posts

I’m trying to insert a block of HTML within the normal list range.

  • Blog Post 1
  • Blog Post 2
  • HTML BLOCK
  • Blog Post 3
  • Blog Post 4
  • Pagination

I’d like to shove a CTA of sorts at a specific point in the loop, and I’d be fine if that showed up on every instance of the list.

I’m not sure what to Google. I’ve tried insert custom HTML within the range and variations of that, but I’m not finding anything.

Any thoughts or ideas welcome! Thank you so much <3

1 Like

Thank you, Joe! This works great!

If I’m reading this right, you’re declaring variable names for an array element’s index and value.

{{ range $k, $v := .Site.RegularPages }}

This bit has me stuck:

{{ if and $k (modBool $k 5) }}

I tried to wrap my brain around this one. I believe it is counting off every 5 .Site.RegularPages , but I don’t understand how the pieces fit together.

I was hoping I could deconstruct it enough to figure out how to limit the number of times it would insert <div>Put Your Banner Here</div>.

Something like this:

  • Blog Post 1
  • Blog Post 2
  • Blog Post 3
  • Blog Post 4
  • Blog Post 5
  • Put Your Banner Here
  • Blog Post 6
  • Blog Post 7
  • Blog Post 8
  • Blog Post 9
  • Blog Post 10
  • Blog Post 11 (no Put Your Banner Here)
  • Pagination

Thanks again! This has been a huge help!!

$k starts at 0, 1, 2 , 3. modBool $k 5 returns true when $k is 0, 5, 10, 15 …

The first and $k is to prevent printing in the first iteration (because 0 is considered “falsy”), so

{{ if and $k (modBool $k 5) }}
{{ end }}

Will happen when $k is 5, 10, 15, 20, 25 …

1 Like

Thank you @bep — that is a whole new world of thinking to me!

I’m clearly missing some core concepts. Looking at the original statement I figured I could try something like this:

{{ if $k == 5 }}

Which does not work at all. :sweat_smile:

Alas, I’m going to position the count so that the paginate config limits the display of my HTML block 1× per rendering of the list:

paginate = 13 and (modBool $k 7)

As you might imagine, I’m not a developer, so thanks again for the detailed explanation! It was super helpful!

Go templates are … functional, so the above would be:

{{ if eq $k 5 }}

See eq | Hugo

1 Like

Once again, this is super helpful! Thank you so much! I’ll be Googling what functional means in this context. Thank you thank you!! :pray:

Posting my final solution here in case anyone else finds this helpful:

{{ range $article_index, $article_array := $paginator.Pages }} 
  {{ if eq $article_index 7 }}
    <div>Put Your Banner Here</div> 
  {{ end }}
  <a href="{{.RelPermalink}}">{{.Title}}</a>
{{ end }}

I suspect you will get unrelated content if you Google that. But the key part here is that every operator and most keywords is a function (eq, and, or etc.).

1 Like

Between the link to the docs you shared (eq) and a quick google, things are making a bit more sense! Thanks again!

Between the clarity of the code and the community here, transitioning to Hugo has been a joy! I was on an old version of Middleman, tried to upgrade and couldn’t figure it out. Then tried to migrate to Gatsby, got 80% done, but couldn’t figure out that last bits. Switched to Hugo and had my entire site redone in less than a month. Thank you, Hugo and the awesome community here! <3

1 Like

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