We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3785
    • 143 Posts
    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
      Medianotions – Studio für Webdesign
      http://www.medianotions.de
      • 22303 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’).
        • 3785
        • 143 Posts
        Thanks a lot! I knew I must be easy smiley
          Medianotions – Studio für Webdesign
          http://www.medianotions.de
          • 27376
          • 576 Posts
          Or you can change the ’global’ in your function to ’static’ and it should work.

          Check out Static Keyword and Variable Scope for more information.

          <?php
          function toggleColor() {
          	static $color;
           
          	if($color=="grey")
          		$color="white";
          	else	$color="grey";
          
          	return $color;
          } ?>
          
            • 3785
            • 143 Posts
            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" huh

            $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!
              Medianotions – Studio für Webdesign
              http://www.medianotions.de
              • 27376
              • 576 Posts
              You’re correct in calling your snippet un-cached. I however recommend against using the API Function, getPlaceholder in this instance as you don’t need placeholder functionality. Try replacing your snippet with this code:
              <?php
              if (!function_exists('toggleColor')) {
              function toggleColor() {
              	static $color;
               
              	if($color=="grey")
              		$color="white";
              	else	$color="grey";
              
              	return $color;
              }}
              return toggleColor();
              ?>


              Since functions can only be defined once and you’re including the snippet more than once I enclosed the function in an ’if’ statement. After that, I return the output of the function to the MODx parser and it should alternate smiley

              When copying and pasting, make sure you don’t have any duplicate <?php tags or anything. Good luck!
                • 3785
                • 143 Posts
                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?
                  Medianotions – Studio für Webdesign
                  http://www.medianotions.de
                  • 27376
                  • 576 Posts
                  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...
                    • 3785
                    • 143 Posts
                    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 smiley

                    Thanks a lot for your help!
                      Medianotions – Studio für Webdesign
                      http://www.medianotions.de