We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21026
    • 53 Posts
    Hi,

    I have little problem with snippet return value.

    In a snippet call ’calendrier’ i run an another snippet call ’ditto_cal_recup’ which should return an array.
    But it returns a string, and the value of this string is : Array.

    here is my code

    ditto_cal_recup code:
    <?php
    if(isset($undoc)){
        $tab_tab_result = array('un','deux','trois','quatre','cinq');
        return $tab_tab_result; 
    }
    ?>
    


    calendrier code:
    <?php
    //id du calendrier des match en court
    $iddoc = $modx->documentIdentifier;
    
    //recuperation des categorie
    $tab_cat = $modx->runSnippet('ditto_cal_recup',array('undoc'=>96));
    
    return 'array? '.is_array($tab_cat). ' nbr '. count($tab_cat). ' value: '. $tab_cat;
    ?>
    


    the snippet calendrier return this:
    array? nbr 1 value: Array

    Am i doing something wrong or maybe it’s not possible to return an array  huh
    If someone could help it will be nice
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It’s returning an array. That "string" you see is just the resource name of what was returned. You need to access the return value as you would with any array ($returned[’fieldname’]). To see exactly what is being returned, give this a try:
      echo "<pre>";
      print_r($returned);
      echo "</pre>";
      

      of course change the $returned to whatever you’ve assigned your inner snippet’s return value to.
        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
        • 21026
        • 53 Posts
        Hello
        And thank you for you help.

        echo "<pre>";
        print_r($returned);
        echo "</pre>
        I try your code and it’s returning: Array undecided

        i don’t know why, but it seems that my array :
        array('un','deux','trois','quatre','cinq')

        turn to that string "Array" >:(

        if i try to access a value $tab_cat[0] it returns: A
        if i try to access a value $tab_cat[1] it returns: r
        .
        .
        .

        I’ve resolve my problem by include the code of the 2nd snippet into the first. grin

        But i’m curious to know why my snippet return didn’t worked! huh

          • 22303 MODX Staff
          • 10,725 Posts
          Snippets must return strings when executed as tags inside of content; this is why it is happening. The only way you could make use of an array as a return value from a snippet is by executing directly via API...
          MISLEADING CODE EXAMPLE REMOVED

          ...which you could easily use as a wrapper snippet, but you ultimately have to return a string for snippets being executed within MODx content.
            • 21026
            • 53 Posts
            Quote from: OpenGeek at Sep 26, 2008, 05:02 PM

            Snippets must return strings when executed as tags inside of content; this is why it is happening. The only way you could make use of an array as a return value from a snippet is by executing directly via API...

            I think, I do execute my snippet directly via API
            part of "calendrier" snippet code:
            //recuperation des categorie
            $tab_cat = $modx->runSnippet('ditto_cal_recup',array('undoc'=>96));
            ...
            foreach($tab_cat as $value)
                 ......
            

            I try to use $tab_cat as an array inside my main snippet, but i can’t.
            I get an error on the foreach, cause it’s not an array.
              • 22303 MODX Staff
              • 10,725 Posts
              Ugh, yeah, nevermind my previous post. It’s being converted to string automatically in the internal function evalSnippets() because it’s trying concatenate any output captured in an output buffer with the return value of the eval().

              The only way of doing this is to have the snippet set a placeholder value...
              <?php
              if(isset($undoc)){
                  $tab_tab_result = array('un','deux','trois','quatre','cinq');
                  $modx->setPlaceholder('tab_cat', $tab_tab_result);
              }
              ?>

              ...and then use that placeholder in the wrapping snippet...
              <?php
              //id du calendrier des match en court
              $iddoc = $modx->documentIdentifier;
              
              //recuperation des categorie
              $modx->runSnippet('ditto_cal_recup',array('undoc'=>96));
              $tab_cat = $modx->getPlaceholder('tab_cat');
              ...
              foreach($tab_cat as $value)
                   ......
              ?>
                • 21026
                • 53 Posts
                Thanks a lot for your explanation

                now i understand grin

                One more question, is it possible to write functions into a snippet code?
                I wasn’t able to make it work, so i used a snippet instead.
                What’s best for repetitive tasks inside snippet code, a function or a snippet call?

                Thanks again, and sorry for my English, i try to do my best.
                  • 3749
                  • 24,544 Posts
                  Quote from: nauar at Sep 26, 2008, 05:50 PM

                  Thanks a lot for your explanation

                  now i understand grin

                  One more question, is it possible to write functions into a snippet code?
                  I wasn’t able to make it work, so i used a snippet instead.
                  What’s best for repetitive tasks inside snippet code, a function or a snippet call?

                  Thanks again, and sorry for my English, i try to do my best.

                  Snippets are php code so anything you can do in php you can do in a snippet. Functions are fine there just as they would be in any php code.

                  Generally, if you want to use a php function, you have to put it inside a snippet in MODx.

                  See the site in my sig. for more information on how MODx works.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 21026
                    • 53 Posts
                    See the site in my sig. for more information on how MODx works.
                    Ok, i’ll read that very soon!
                    But it’s bed time in france.
                    So... Ladies and gentleman... Good night! wink
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Interesting. I’ve used snippets to return arrays before; I must have done something that I forgot about later.

                      One thing to know when making functions inside of snippets is that you have to put
                      global $modx;

                      at the beginning of your function code.
                        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