Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-11-19 01:22:41

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,228
Website

txp:variable increment & decrement

How about adding a couple of extra attributes to <txp:variable /> to enable counters to incremented or decremented.

For example, <txp:variable name="counter" inc="1" /> would add 1 to the value of counter and <txp:variable name="counter" dec="2" /> would subtract 2 from its value.

So code, such as that given as an example in the tag reference for <txp:variable />, could then be shortened from:

<txp:if_variable name="trigger">

  <txp:if_variable name="trigger" value="2">
    <txp:variable name="trigger" value="3" />
  </txp:if_variable>

  <txp:if_variable name="trigger" value="1">
    <txp:variable name="trigger" value="2" />
  </txp:if_variable>

<txp:else />
  <txp:variable name="trigger" value="1" />
</txp:if_variable>

to:

<txp:if_variable name="trigger">
  <txp:variable name="trigger" inc="1" />
<txp:else />
  <txp:variable name="trigger" value="1" />
</txp:if_variable>

which is more elegant and more flexible.

Offline

#2 2008-11-19 01:39:46

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: txp:variable increment & decrement

:) I’m the author of magic example, you quoted. And agree with you – adding increment\decrement would decrease the size of code in this example. But are there any other usages for inc\decr?


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#3 2008-11-19 02:01:02

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,228
Website

Re: txp:variable increment & decrement

the_ghost wrote:

:) I’m the author of magic example, you quoted. And agree with you – adding increment\decrement would decrease the size of code in this example. But are there any other usages for inc\decr?

Hi Victor. It would not only decrease the size but make it less error prone as well. Imagine the code required to extend your example from 3 columns to 10. It also would enhance TXP variables from being simple data storage into a fully fledged counters, extending Textpattern’s capability.

Offline

#4 2008-11-19 02:56:39

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,228
Website

Re: txp:variable increment & decrement

Victor, using your example of a table of article titles again. The boxes could be numbered using a counter.

Initialise counter: <txp:variable name="count" value="0" />

Then wherever an article title is output:

<txp:variable name="count" inc="1" />
<td><txp:variable name="count" />. <txp:title /></td>

So you’d end up with 1. xxxxxx, 2. yyyyyy, 3. zzzzzz etc.

Offline

#5 2008-11-19 03:00:25

maniqui
Moderator
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: txp:variable increment & decrement

I would bet this can be done (although I’ve to think exactly how) with this recipe:

  • 1 <txp:variable />
  • 1 <txp:rvm_counter /> (here) (ideally, using a modified version ruud sent me)
  • tags in tags (at will)
  • magic dust (at will)

Add a <txp:rvm_counter /> inside <txp:variable />’s value attribute. Serve cold.
Like this:

<txp:variable name="even_articles" value='<txp:rvm_counter name="even_counter" step="2" />' />

Current version of rvm_counter does the count on the database, so concurrent hits on a page can lead to an wrong count.
Thankfully, ruud sent me a modified version that does the count “on the fly” (on memory).

So, what do you think? Will this work?


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#6 2008-11-19 05:51:08

wet
Developer
From: Lenzing, Austria
Registered: 2005-06-06
Posts: 3,267
Website

Re: txp:variable increment & decrement

As a guideline, I don’t think <txp:variable /> should be transformed into Textpattern’s built-in desktop calculator by adding a selection of mathematical capabilities to it.

See, as soon as we had inc and dec, it would be a logical next step to add comparisons and string operations and then, maybe, multiplication and division, and so on. Let this be a field where <txp:php> earns its merits.

Offline

#7 2009-02-23 05:33:59

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,228
Website

Re: txp:variable increment & decrement

wet wrote:

Let this be a field where <txp:php> earns its merits.

… or a plugin: adi_calc.

I had a scenario where I needed to do a number of calculations on a counter and dodging in & out of <txp:php> became too arduous.

Offline

Board footer

Powered by FluxBB