[Typo3-dev] The quest of optionSplit : a first approach

Jean-Baptiste Rio triphot69 at hotmail.com
Tue Nov 15 13:26:31 CET 2005


Every month in the typo3.english list, someone asks for the famous 
question "How to use optionSplit when it's not a menu ?".
The answer is quite simple : "no way for the moment".

But sometimes, it could be very useful to have this option in order to 
alternate styles or color in a table built with rows returned by a 
CONTENT or RECORDS object.

So i tried to find a solution for this high quest based on one simple 
rule : no Typo3 core hack.

The goal :
- Build a CONTENT object and render it as a table
- Alternate line styles

The result :

First of all, i need to count each line. Is it possible in typoscript ?
=> Yes, with an undocumented getText property : *cobj : parentRecordNumber*
This property is set as the row number (1,2,3,4,5...) for CONTENT and 
RECORDS results

So my typoscript is :

20 = CONTENT
20 {
      table = tx_myext_mytable

renderObj = COA
renderObj {
   stdWrap.innerWrap = <tr> | </tr>
   stdWrap.innerWrap.addParams.class = |
   stdWrap.innerWrap.addParams.class.data = cobj : parentRecordNumber
   stdWrap.innerWrap.addParams.class.postUserFunc = user_optionSplit
   stdWrap.innerWrap.addParams.class.postUserFunc.optionSplit = 
|*|odd||even|*|

...

}

And the php function is light version of the menu optionSplit function :

function user_optionSplit($content, $conf) {

		// Initialize variables:
	$content = intval($content);

	if ($content && is_array($conf))	{

		$val = $conf['optionSplit'];
			// Splitting of all values on this level of the TypoScript object tree:
		if (!strstr($val,'|*|') && !strstr($val,'||'))	{
			return $val;
		} else {
			$main = explode ('|*|',$val);
			$mainCount = count($main);

			$lastC = 0;
			$middleC = 0;
			$firstC = 0;

			if ($main[0])	{
				$first = explode('||',$main[0]);
				$firstC = count($first);
			}
			if ($main[1])	{
				$middle = explode('||',$main[1]);
				$middleC = count($middle);
			}
			if ($main[2])	{
				$last = explode('||',$main[2]);
				$lastC = count($last);
				$value = $last[0];
			}

			$aKey = $content - 1;
			if ($firstC && isset($first[$aKey])) {
				$value = $first[$aKey];
			} elseif ($middleC) {
				$value = $middle[($aKey-$firstC)%$middleC];
			}
			if ($lastC && $lastC>=($content-$aKey))	{
				$value = $last[$lastC-($content-$aKey)];
			}
			return trim($value);
		}
	}
	return $content;
}

You have to include this function in a library and include your library 
in the TS template :

#include library
includeLibs.commonLib = fileadmin/common/my_option_split_lib.php

That's all !!

Enjoy :)

Jean-Baptiste





More information about the TYPO3-dev mailing list