We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11316
    • 81 Posts
    I was wondering if anyone would could help me with getting something to check for multiply arrays. I am learning as I go on the the dev part but I hoping for some guidance.

    The idea
    $tvArray = $modx->getTemplateVarOutput(array(’firstImage’)); Single instance
    $image1 = $tvArray[firstImage];
    if (empty($image1)){
    $output = "";
    } // the rest as follows

    I have been trying to achieve the following
    $tvArray = $modx->getTemplateVarOutput(array(’firstImage’,’secImage’)); Multiply instances
    $image1 = $tvArray[firstImage];
    $image2 = $tvArray[secImage];
    if (empty($image1)){
    $output = "";
    if (empty($image2)){
    $output = "";

    else {
    $output =’<img src="’.$image1.’" width="380" height="300" />’;
    }$output =’<img src="’.$image2.’" width="380" height="300" />’;

    I am sure some coders will get the idea of what I am trying to achieve. Not just looking for the exact code but more of this how you achieve and why it works.
    Wanting to learn how to develop more these ideas out. Dunce cap firmly seated on my head ready to learn.
      • 4310
      • 2,310 Posts
      This isn’t fully tested but a quick print_r showed the correct array values, it may need a little tweaking laugh
      <?php
      $output = '';
      $myArray = $modx->getTemplateVarOutput(array('firstImage','secondImage','thirdImage'));
       if(!empty($myArray['firstImage'])){
       $output .='<img src="'.$myArray['firstImage'].'" width="380" height="300" />';
       }
       if(!empty($myArray['secondImage'])){
       $output .='<img src="'.$myArray['secondImage'].'" width="380" height="300" />';
       }
       if(!empty($myArray['thirdImage'])){
       $output .='<img src="'.$myArray['thirdImage'].'" width="380" height="300" />';
       }
       return $output;
      ?>
        • 3749
        • 24,544 Posts

        I think you might also ba able do it with PHX without any snippet.

        Something like this:

        [*tv1:ifempty=`[*tv2:ifempty=`[*tv3*]`*]`*]


        You’d have to have PHX installed. I think the snippet would be faster.
          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
          • 11316
          • 81 Posts
          Thank you both for the very helpful replies.

          My lesson of the day was understanding -if(!empty($myArray[’firstImage’]- so from what I can understand, is that instead of using if empty output"" this call instead use the same effect with less code. Key point bieng !empty. I feel that I have so much to learn.

          I have yet to tread the PHx. I just haven’t had enough time to sit down and get prepped but it is slowly becoming more aware of using it from other examples. laugh

          You’d have to have PHX installed. I think the snippet would be faster. The Snippet is extremely fast though!
            • 11316
            • 81 Posts
            There is an old saying "give a man a fish and he can eat for day, teach a man to fish and he can eat for life"

            bunk58 I have to say thank you for this wonderful script, now that I am taking the time to progressively learn (if else) statements I give this code back enhanced one more time.

            <?php
            $output = '';
            $myArray = $modx->getTemplateVarOutput(array('home5','homeLink5','home6','homeLink6'));
            	if(empty($myArray['home5'])){
            	} elseif (empty($myArray['homeLink5'])) {
            	$output .= '<img src="'.$myArray['home5'].'" width="270" height="210" />';
            	} else {
            	$output .= '<a href="[~'.$myArray['homeLink5'].'~]"><img src="'.$myArray['home5'].'" width="270" height="210" /></a>';
            	}
            	if(empty($myArray['home6'])){
            	} elseif (empty($myArray['homeLink6'])) {
            	$output .= '<img src="'.$myArray['home6'].'" width="270" height="210" />';
            	} else {
            	$output .= '<a href="[~'.$myArray['homeLink6'].'~]"><img src="'.$myArray['home6'].'" width="270" height="210" /></a>';
            	}
             return $output;
            ?>
              • 4310
              • 2,310 Posts
              Glad you’re getting to grips with the power of snippets laugh
                • 11316
                • 81 Posts
                Hey Bunk, don’t know if you could me on this.

                I am trying to use the same script above but hitting a wall- I know why but I don’t know how to get around it.

                I am using a ditto call on container [[Ditto? &tpl={{chunk}}]]

                Chunk
                {{[+pagetitle+] [[SnippetTVArray]]}}

                I tried changing getTemplateVarOutput -> getDocumentChildrenTVarOutput. Outputs nothing, after reading the API getDocumentChildrenTVarOutput works but the array uses the ID as the key in the array. Which I am trying to figure out how to merge the arrays - remove the id and make the arrays without keys. Am I crazy???

                Since there is no getChildrenTemplateVarOutput....

                Any help would be appreciated!
                  • 4310
                  • 2,310 Posts
                  Normally if you run one snippet inside another you have to alternate the caching e.g.
                  [[Ditto? &tpl={{chunk}}]] & {{[+pagetitle+] [[SnippetTVArray]]}}
                  to :
                  [!Ditto? &tpl={{chunk}}!] & {{[+pagetitle+] [[SnippetTVArray]]}}
                  Don’t know if that will solve your problem.
                    • 11316
                    • 81 Posts
                    I think I confused the situation.

                    Here is my snippet that only works on the actual page.

                    --doc 1 [[Snippet]]

                    <?php
                    $output = ’’;
                    $myArray = $modx->getTemplateVarOutput(array(’tv1’,’tv2’));
                    if(empty($myArray[’tv1’])){
                    } else {
                    $output .= ’ ’.$myArray[’tv1’].’ ’;
                    }
                    if(empty($myArray[’tv2’])){
                    } else {
                    $output .= ’ ’.$myArray[’tv2’].’ ’;
                    }
                    return $output;
                    ?>

                    What I am trying to achieve.

                    - Folder 1 - no tv’s (Template 1)
                    -- doc 1 - tv 1 - tv 2 (Template 2)
                    -- doc 2 - tv 1 - tv 2 (Template 2)

                    -Folder 1 [[Ditto? &tpl=`myTpl`]]

                    If place the [[SnippetAbove]] in myTpl - nothing outputs.

                    Thank you so much for taking the time!