We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29413
    • 8 Posts
    I’m having a problem which is very frustrating because I can code ok but I’m fairly new to modx and the logic is just a bit different and therefore confusing for me.
    I want to make a sort of panels in my layout. The user must have the ability to add panels so therefore I am writing now a snippet that implements a chunk for every panel.

    The snippet call:
    [!panel? &titel=`Header Text` &kolom=`1`!]


    So far so good. Then I want that the user can edit the content of these panels in the front-end-editor (Quickedit I believe it’s called?). So I probably have to attach TV’s to every panel. My solution is to have a counter in the snippet and with that the reference to the TV is changing like "reference1", "reference2", "reference3", and so on. This reference is then placed into the placeholder.
    This doesn’t seem to work. What is the strategy here? Does someone has a hint for me in what direction I have to look?

    The snippet code: (a bit messy because all the weird things I’ve tried)
    <?php
    if(function_exists(panelFunc)) { 
            ++$kloten;
    } else {
            $kloten = 1;
            function panelFunc() {          
            }
            panelFunc();
    }
    
    $ouput = '';
    $panelKolom = 0;
    $pan_str_K = '';
    $pk = '';
    $panelKolom = $kolom;
    $pan_str_K = $titel;
    
    $klotenStr = (string)$kloten;
    $panelEdit = '[*#vak';
    $panelEdit .= $klotenStr;
    $panelEdit .= '*]';
    
    
    switch ($panelKolom)
    {
    case 1:
      $pk = 'left';
      break;
    case 2:
      $pk = 'middle';
      break;
    case 3:
      $pk = 'right';
      break;
    default:
      echo "Het is alleen mogelijk om vakken in de 1e, 2e of 3e kolom te zetten";
    }
    
    
    // Set placeholders:
    $modx->setPlaceholder('kop', $pan_str_K);
    $modx->setPlaceholder('pStyle', $pk);    
    $modx->setPlaceholder('edit', $panelEdit);        
    
    // set chunk name in this example it is named addressBook
    $myChunk = "tplPanel";
    
    // parse the chunk and replace the placeholder values
    $values = array('kop' => $pan_str_K, 'pStyle' => $pk, 'edit' => $panelEdit);
    
    // run the parseChunk function
    $output = $modx->parseChunk($myChunk, $values, '[+', '+]');
    
    return $output;
    ?>

      • 29413
      • 8 Posts
      Hmm, nobody?
      Let me put it this way:
      Why isn’t this working when I call this multiple times on a page?
      $panelCounter = isset($panelCounter) ? 2 : 1;
      return $panelCounter;
      


      It is just keep giving me ’1’!!

      P.S. I did disable cache and use [!panelCounter!]
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Snippet code is passed through the PHP eval() function, so snippets behave much the same way functions do in PHP (which is why they are ended with return statements). In the case of
        $panelCounter = isset($panelCounter) ? 2 : 1;
        return $panelCounter;

        $panelCounter is local to the instance of the snippet, and therefor never set for other instances of the snippet. That structure is used in most snippets to determine if a parameter or configuration setting is used, and sets a default in case it isn’t.

        If a value needs to be used elsewhere in a resource, you can insert it into the $modx->placeholders array, then use the placeholder tags or $modx->getPlaceholder() wherever it’s wanted. Or load it into the PHP $_GLOBALS array, or even the $_SESSION to pass it from page to page.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 29413
          • 8 Posts
          Thanks a million!
          I’m going to try some more. Can you check this thread sometimes?
            • 29413
            • 8 Posts
            Ok I tried this but it doesn’t work right.
            $going= '';
            if(isset($id)) {
            $going = $id;
            unset($id);
            } else {
            $going = $modx->getPlaceholder('counter');
            }
            $going++;
            $modx->setPlaceholder('counter',$going);
            return $going;


            When I call the snippet 3 times, the output is "1 2 2".
            For now I’ve set the parameter ’id’ in the first call only. Not very nice I guess but I don’t know how to init this differently.

            Any thoughts?
              • 29413
              • 8 Posts
              Allright, it worked after some fiddling.
              I had to parse the placeholder.
              The relevant line in the chunk is:
              [*panel[+counter+]*]