Categories > TinyButStrong general >

Make global template variable counter

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Martin
Date: 2010-05-13
Time: 22:38

Make global template variable counter

Hi Skrol !!

How do you do, if you want to make a counter to use in <code>tabindex</code> of html elements ?

I think something like this

In php file declare a variable named: <code>$tabindex = 0</code>
In tpl file put in every html tag, for example in SELECT tag:

<code><select name="sel" id="sel" tabindex="[onshow.tabindex;ope=add:1]"></code>

So if, i have 50 form elements and change the orders of element, the tabindex properties it will be automatic.

I thinking to execute a custom function and do $tabindex++; and return the value, but i dont know if this is the best practice.

Thanks a lot !!!

Martin
By: Skrol29
Date: 2010-05-14
Time: 10:13

Re: Make global template variable counter

You can do it using an onformat function.
Here is the code I would use (not tested) :

PHP:
function tbs_counter($FieldName,&$CurrVal,&$CurrPrm) {
   // onformat function which permforms counter in the template
   $var = (isset($CurrPrm['counter'])) ? 'tbs_counter_'.$CurrPrm['counter'] : 'tbs_counter';
  if (!isset($GLOBALS[$var])) {
     $GLOBALS[$var] = (isset($CurrPrm['start'])) ? intval($CurrPrm['start']) : 1;
   } else {
    $GLOBALS[$var]++;
   }
  $CurrVal = $GLOBALS[$var];

}

HTML:
basic : [onshow;onformat=tbs_counter]
with a start value : [onshow;onformat=tbs_counter;start=7]
several counters in  the same template : [onshow;onformat=tbs_counter;counter=c2]