Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-09-24 16:42:57

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,396
Website

Count images available and then store that as a variable.

Hi.

I want to count the number of images available in category (up to a maximum limit of 12) and then store that count in a variable.

So basically in this loop…

<txp:images category="gallery" break="" sort="date desc" limit="12">
    <li><img loading="lazy" src="<txp:image_url />" alt="<txp:image_info type='alt' />" itemprop="image"></li>
</txp:images>

…add +1 to counter like <txp:variable name="number-of-images" /> (or something more performant if you think there’s something better) for each image that is available.

Then later on, output the following code the same number of times…

<button data-glide-dir="=0">1 <txp:text item="of" /> 3</button>

…based on that count.

However, also within that second snippet the data-glide-dir="=0" would need to increment +1 each time starting from 0, and the 1 <txp:text item="of" /> 3 would actually need to be along the lines of {this image number} <txp:text item="of" /> {total images counted}.

Is there any way to do this? Hopefully I’ve explained it as best I can?

Cheers!

Offline

#2 2020-09-24 17:28:14

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,789
Website

Re: Count images available and then store that as a variable.

That certainly is fun! Counting is easy:

<txp:images category="gallery" break="" sort="date desc" limit="12">
   <txp:variable name="number-of-images" add="1" />
</txp:images>

But once you have that value it’s not much use as you can’t iterate over it natively.

What I’d be tempted to do would be to build the output you want on the fly in the <txp:images> loop using the <txp:variable> concatenation feature of the add attribute:

<txp:hide> Initialize counters </txp:hide>
<txp:variable name="image-number" value="0" />
<txp:variable name="image-limit" value="12" />

<txp:hide> Build image output via concatenation </txp:hide>
<txp:images category="gallery" break="" sort="date desc" limit='<txp:variable name="image-limit" />'>
   <txp:variable name="image-output" add='<button data-glide-dir="=<txp:variable name=''image-number'' />"><txp:variable name=''image-number'' add="1" output /> <txp:text item="of" /> <txp:variable name=''image-limit'' /></button>' />
</txp:images>

Then at the end, dump the output where you want it:

<txp:variable name="image-output" />

Last edited by Bloke (2020-09-24 17:32:31)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#3 2020-09-24 17:57:17

etc
Developer
Registered: 2010-11-11
Posts: 3,940
Website

Re: Count images available and then store that as a variable.

You can also call secondpass to the rescue:

<txp:images category="gallery" break="" sort="date desc" limit="12">
    ...
    Image <txp:variable name="number-of-images" add output /> of
    <txp:hide process="2"><txp:variable name="number-of-images" /></txp:hide>
    ...
</txp:images>

Edit: forgot output.

Offline

#4 2020-09-24 18:08:47

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,789
Website

Re: Count images available and then store that as a variable.

Ooh, I like that! Nice.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#5 2020-09-25 08:47:21

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,396
Website

Re: Count images available and then store that as a variable.

Thanks guys, this is great stuff.

Offline

Board footer

Powered by FluxBB