Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-01-27 02:54:48

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,639
GitHub Twitter

TXP variable and counting

I’ve got a strange behavior with variable.

I need a short code in order to inject only one, JavaScript call:

<txp:variable name="gallery_counter" add />
<div class="image-gallery<txp:variable name="gallery_counter" />">


</div>
<txp:if_variable name="gallery_counter" value="1">
<script>
// some code...
</script>
</txp:if_variable>

The short code works, but… the count starts to… 7. 😱

Sure, I’m using other variables in my page (maybe 6, that’s right).

Question: How can I get a counter starting from 1? (TXP 4.9-dev)

Last edited by Pat64 (2024-01-27 03:50:27)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#2 2024-01-27 07:43:08

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,540
Website GitHub Twitter

Re: TXP variable and counting

Hi Patrick

Where have you initilized the variable?

<txp:variable name="gallery_counter" value="0" />

Offline

#3 2024-01-27 08:58:30

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,639
GitHub Twitter

Re: TXP variable and counting

Yep. I tried (based on this post, we don’t need to initialize a variable)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#4 2024-01-28 08:43:50

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,743
Website

Re: TXP variable and counting

That’s a weird-sounding one. I tried to reproduce it by putting the following

<txp:variable name="demo_counter" add />
<txp:variable name="sample_counter" add />
<txp:variable name="test_counter" add />
<li class="article" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
    <h4 itemprop="headline" id="article<txp:variable name="test_counter" />">
        <a href="<txp:permlink />" itemprop="url mainEntityOfPage" title="<txp:text item="read_more" />">
            <txp:title /> – <txp:variable name="test_counter" /> ( <txp:variable name="demo_counter" /> ) { <txp:variable name="sample_counter" /> }
        </a>
    </h4>

<txp:if_variable name="test_counter" value="1">
     <h3>First counter</h3>
</txp:if_variable>
…

in the article_listing form of the demo site, but I get the expected output:

Some wild guesses:

  • Try giving the counter name an entirely different name. Does it still happen?
  • Could the form in which the counter is advanced be being called multiple times?
  • Could the counter be advancing for every article but your galleries only appear on some articles, e.g. the first gallery happens to be on the seventh article in the list?
  • Try outputting <txp:php>global $variable; dmp($variable);</txp:php> at a strategic place on the page to see if that gives you any clues.

TXP Builders – finely-crafted code, design and txp

Online

#5 2024-01-28 09:18:40

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,639
GitHub Twitter

Re: TXP variable and counting

Hi Jacob.
Thank you very much for your efforts.
All my variables have unique names.
You are right: the counter takes the article number as its starting point!


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#6 2024-01-28 09:29:37

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,743
Website

Re: TXP variable and counting

Pat64 wrote #336478:

You are right: the counter takes the article number as its starting point!

Hehe, I’ve done that before too!!


TXP Builders – finely-crafted code, design and txp

Online

#7 2024-01-28 12:47:42

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,639
GitHub Twitter

Re: TXP variable and counting

jakob wrote #336479:

Hehe, I’ve done that before too!!

So what is the solution?


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#8 2024-01-28 13:16:10

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,743
Website

Re: TXP variable and counting

Pat64 wrote #336481:

So what is the solution?

Advance the counter only in the cases where you have a gallery. If, for example, you have a gallery when you have more than one article image, then test for offset="1" limit="1" of your article_image, for example

    <txp:evaluate query='<txp:custom_field name="article_image" offset="1" limit="1" />'>
        <txp:variable name="gallery_counter" add />
        <h5>gallery #<txp:variable name="gallery_counter" /></h5>
        <txp:if_variable name="gallery_counter" value="1">
            <h6>first counter</h6>
        </txp:if_variable>
    </txp:evaluate>

Testing on the demo site – with four articles where articles two and four have three and two image ids in the article_image field – I get the following:


TXP Builders – finely-crafted code, design and txp

Online

#9 2024-01-28 14:16:14

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,639
GitHub Twitter

Re: TXP variable and counting

Jacob, this is fabulous!

You win (another) gold medal for the best Textpattern user of the day. 🥇

Thank you lot. ;)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

Board footer

Powered by FluxBB