Hello everybody,
I am quite new to MODx and can’t get a simple toggle snippet to work. Please be so kind to help ...
That is how the simple function looked like before MODx:
function toggleColor($color)
{
global $color;
if($color=="grey") $color="white";
else $color="grey";
return $color;
}
I used this function to toggle the color of HTML table rows.
I tried to write a snippet with a similar function but soon realized, that I have no place to store the color information since a variable $color is lost next time I call the snippet.
Any idea how I can get this to work with MODx?
Thanks
Bogdan
-
MODX Staff
- 10,725 Posts
You can set $GLOBALS[’color’] directly and work with that within your scripts, or you can use MODx placeholders by storing it with $modx->setPlaceholder(’color’, $color); and then using it in your templates/chunks/content as [+color+], or accessing it from another snippet with $modx->getPlaceholder(’color’).
Thanks a lot! I knew I must be easy
I am a little embarrassed to admit, that my toggle snippet still does not work. Is the snippet executed maybe only once and then cached by MODx? I thought by calling the snippet with [!snippet!] this would be impeded?
My snippet looks like this now but returns only "grey"
$color=$modx->getPlaceholder('color');
if($color=="white" or !$color) $color="grey";
else $color="white";
$modx->setPlaceholder('color', $color);
echo($color);
In the document I call it in this way:
<table>
<tr class="[!toggleTableRowColor!]"><td>some data</tr>
<tr class="[!toggleTableRowColor!]"><td>some more data</tr>
<tr class="[!toggleTableRowColor!]"><td>and more</tr>
<tr class="[!toggleTableRowColor!]"><td>and more</tr>
...
</table>
Thanks for your feedback!
Thanks for the code. I copied it 1:1 but still the same result, the snippet returns alwas "grey" as can be seen here:
http://abm.medianotions.de/de/service/haendler-in-deiner-naehe/plz-bereich-00000-09999
Any ideas how to get such a toggle snippet that is called multiple times in one document to run properly?
I just had a D’oh! moment...
This is a limitation of the MODx parser, you cannot have multiple snippet calls to the same snippet return different results. The parser will look through the content and replace all instances of the snippet call at once (meaning on your page, the snippet is called ONCE). In order to work around this you need to pass a parameter to the snippet that is different in each call, eg. a row number or something.
Of course, there are other alternatives, but they escape me at the moment...
Ah ... that is an important bit of information. No wonder all my attempts failed! I call my snippet like this now:
<table class="dealers">
[[printTableRow? &content=`<td>UGT-Bikes Dresden GmbH<td>01159<td>Dresden<td>0351/2517800`]]
[[printTableRow? &content=`<td>Hartig Instruments GmbH<td>01558<td>Großenhain<td>07576/9609-80`]]
[[printTableRow? &content=`<td>Scheibe<td>01920<td>Säuritz<td>035953/6379`]]
[[printTableRow? &content=`<td>Liebe<td>02791<td>Oderwitz<td>035842/39427`]]
[[printTableRow? &content=`<td>Darkfighter<td>02977<td>Hoyerswerda<td>03571/607200`]]
...
Now everything works fine
Thanks a lot for your help!