Every month in the typo3.english list, someone asks for the famousquestion "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
Well - nice patch since it will be working in other places as well, but "noway" is not true.
You can of course use optionSplit outside of menus just by using
stdWrap.split.
So you can do something like this:
tt_content.stdWrap.outerWrap = |###SPLITTER###
temp.mySplittedContent = COA
temp.mySplittedContent {
10 < styles.content.get
stdWrap.split {
token = ###SPLITTER###
cObjNum = 1 || 2 || 3
1.current = 1
1.wrap = <div class="mycontent1">|</div>
2.current = 1
2.wrap = <div class="mycontent2">|</div>
3.current = 1
}
}
}
The rest will be done with appropriate CSS and without any patch.
Just one example how to use it. You can imagine there are much more
possibilities of using the same concept in other places as well.
Joey
Well - a little rewrite does the trick:
temp.mySplittedContent = COA
temp.mySplittedContent {
10 < styles.content.get
stdWrap.split {
token = ###SPLITTER###
cObjNum = 1 || 2 || 3 |*| 4 || 5 |*| 6
1.current = 1
1.wrap = <div class="mycontent1">|</div>
2.current = 1
2.wrap = <div class="mycontent2">|</div>
3.current = 1
3.wrap = <div class="mycontent3">|</div>
4.current = 1
4.wrap = <div class="mycontent4">|</div>
5.current = 1
5.wrap = <div class="mycontent5">|</div>
6.current = 1
}
}
}
This one will work for an unknown number of entries.
With 12 entries you will get:
1 2 3
4 5 4 5 4 5 4 5
6
BTW: the original example would result in:
1 2 3 3 3 3 3 3 3 3 3 3
which is working for an unknown number of entries too, but not that
flexible.
Joey