Range after a random number of posts between 1 - 10

Hello,

i want to inject an ad between posts, lets say after 3 posts i get an ad and in the next page after 5 posts i get the ad etc… i have 10 posts per page

this is how i generate my posts

{{ range where .Paginator.Pages “Params.hidden” “ne” “true” }}
{{ partial “post” . }}
{{ end }}

Basically you can achieve this by using combinations of first, after and last

For example:

First store the page collection in a variable

{{ $pages := where .Paginator.Pages "Params.hidden" "ne" "true" }}

Then split the page collection into more variables according to your needs.

For example for 10 pages I would do:

{{ $firstgroup := first 3 $pages }}
{{ $secondgroup :=  first 5 (after 3 $pages) }}
{{ $lastgroup := last 2 $pages }}

Then

{{ range $firstgroup }}
<--- HTML for posts 1-3 --->
{{ end }}
<--- HTML for 1st advert --->
{{ range $secondgroup }}
<--- HTML for posts 4-8 --->
{{ end }}
<--- HTML for 2nd advert --->
{{ range $lastgroup }}
<--- HTML for posts 9-10 --->
{{ end }}