2

i'm going to create a number based on content collection post, i need to know how many post in one collection:

so i need to create something like this (1/5) / (2/5) / (3/5) / (4/5) / (5/5)

since we can use {.repeated section items}, and also can use {.equal displayIndex 0} to detect the first post

how to print total post in items?

how to print displayIndex? i cannot print the value like this too { displayIndex }

how to define a variable?

{.repeated section items}

{ define variable x = 0} --> im going to do this

{ define x + 1 } --> and this

{.end}

thx...

zho
  • 643
  • 1
  • 12
  • 27

1 Answers1

1

You can define a variable by using the {.var @variable __data__} directive. This workflow is tied directly to the JSON data that Squarespace backend is serving (?format=json-pretty) and I haven't seen a working implementation of passing a custom variable (something you define directly in the code).

Something like {.var @number 1} is not implemented by Squarespace JSON-T.

Squarespace has {@index} variable that carries the Collection position of the item starting with natural 1. In the blog, the most recent posts will have an index of 1.

And finally, each Squarespace collection has a JSON key .itemCount that holds the number of items in that Collection.

Your Solution

{.var @numberOfItems collection.itemCount}

This {.var} definition must be in global scope to make the itemCount value available inside items loop (below).

{.repeated section items}
    {.var @currentItem @index}
    {.if title}
        <h1>{title}<span class="postItemCount">{@currentItem}/{@numberOfItems}</span></h1>
        {##}..additional post data goes here..{##}
    {.end}
{.end}

Notes

  • {.if title} is an optional check for correctness of post data. If there is no title then nothing gets displayed.
  • I've put the count next to the title but you can format it any way you want.
  • You an also add additional check for specific @index and style that span accordingly. Refer to Squarespace template code for examples on how to add parametric classes in JSON-T.
ermik
  • 408
  • 3
  • 17