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

    I’m trying to move my current Modx site from v1 to v2. However, I’m having some troubles with my custom snippet communicating with my chunks. I have a PHP for loop set up and when I do a direct echo of the data, it shows on the page, but then once I set it up to loop through my chunk, it only outputs the very last chunk placeholder I have set up, and even skips (what appears to be) random outputs. Would it be a caching thing?

    You can see here: http://174.121.36.226/~gaia/

    The URL’s and Hover variable data aren’t making it over, and it randomly skipped 2 sections.

    This is my chunk:

    <li><a href="[[+rssUrl]]" title="[[+rssTitleHover]]" class="tooltip">[[+rssTitle]]</a></li>
    


    This is the part of my snippet that’s calling the chunk:

    	      //Assign the data
    	      $data = array(
    	         'rssUrl' => $xml->channel->item[$i]->link,
    	         'rssTitle'  => $truncTitle,
                     'rssTitleHover' => $xml->channel->item[$i]->title,
    	      );
      
                 $output .= $modx->getChunk('rssListResults',$data);


    This is my snippet call ( I don’t think the call has changed any? ):

    [[!rssFetch? &rssFeedLink=`http://url.to.rss` &rssFeedLimit=`5`]]


    Thanks in advance for any insight on this!
      • 10395
      • 96 Posts
      Anyone? sad
        • 10395
        • 96 Posts
        There is something strange going on here.

        The following works:

        //Set the link var
        $link = "http://test.com/";
        
        //Output the raw data
        echo $link;
        
        //Output it via the chunk
        $output = $modx->getChunk('myChunk',array(  
          'name' => $link,  
        ));  


        The following DOESN’T work:

        //Set the link var
        $link = $xml->channel->item[1]->link;
        
        //Output the raw data
        echo $link;
        
        //Output it via the chunk
        $output = $modx->getChunk('myChunk',array(  
          'name' => $link,  
        ));  


        I don’t see why it wouldn’t get passed through to the chunk, the echo outputs the correct thing.

        Any ideas?
          • 3749
          • 24,544 Posts
          I don’t see any reason why it wouldn’t work (assuming that you have return $output at some point.

          You might try taking out the comma after $link, but it shouldn’t matter.
            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
            • 10395
            • 96 Posts
            Thanks for the reply. Wouldn’t it be considered a bug? I’m not quite sure what difference it would make between a var generated by XML or just a regular manually declared var. Even when I echo the $xml var directly, it outputs the correct thing. Modx just won’t pass it to the chunk for some reason.
              • 3749
              • 24,544 Posts
              Quote from: Gaia at Aug 10, 2010, 12:49 PM

              Thanks for the reply. Wouldn’t it be considered a bug? I’m not quite sure what difference it would make between a var generated by XML or just a regular manually declared var. Even when I echo the $xml var directly, it outputs the correct thing. Modx just won’t pass it to the chunk for some reason.


              It would be a bug, but so many others are having no trouble with getChunk() that it seems unlikely. Try using a variable name/placeholder name other than "link."
                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
                • 10395
                • 96 Posts
                Ok, I removed all my extra code and just have the basic XML function, I also changed $link to $http and still nothing. Here is the exact code I’m using:

                Snippet:
                <?php
                //Start cURL connection
                	    $ch = curl_init($rssFeedLink);
                           
                	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                	    curl_setopt($ch, CURLOPT_HEADER, 0);
                
                	    //Fetch RSS data and close the connection
                	    $data = curl_exec($ch);
                	    curl_close($ch);
                
                	    //Start up SimpleXML class
                	   $xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
                               
                $http = $xml->channel->item[0]->link;
                $title = $xml->channel->item[0]->title;
                
                	$output = $modx->getChunk('myChunk',array(  
                  'name' => $http
                ));
                ?>
                


                Chunk:
                [[+name]]
                  • 3749
                  • 24,544 Posts
                  You might want to put this line at the end:

                  return $output;


                  Otherwise, you won’t get any output.

                  I don’t mean to be critical, but you should really be more careful about using names you can be sure are unique for variables and placeholders (or, better yet, add prefixes to them: $myRSS.link). It will bite you eventually -- like if some other snippet on the page (or a plugin) is also setting a ’name’ placeholder, for example. I think $http might be worse than $link in that regard. wink



                    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
                    • 10395
                    • 96 Posts
                    Ah crap, I’m starting to get to that crazed stage where I miss simple things tongue.

                    Well, I did some updates, but still not returning anything sad. Here’s the code (hopefully I didn’t miss something again smiley )

                    Snippet:
                    <?php
                    //Start cURL connection
                    	    $ch = curl_init($rssFeedLink);
                               
                    	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    	    curl_setopt($ch, CURLOPT_HEADER, 0);
                    
                    	    //Fetch RSS data and close the connection
                    	    $data = curl_exec($ch);
                    	    curl_close($ch);
                    
                    	    //Start up SimpleXML class
                    	   $xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
                                   
                    $rssResultLink = $xml->channel->item[0]->link;
                    
                    echo "Echo: ".$rssResultLink;
                    
                    	$output = $modx->getChunk('myChunk',array(  
                      'myRssLink' => $rssResultLink
                    ));
                    
                      return $output;
                    


                    Chunk:

                    <br />Chunk Result: [[+myRssLink]]


                    Results can be found here: http://174.121.36.226/~gaia/. You can see the "Echo" section outputs a URL, but the Chunk Result outputs nothing.

                    Thanks again for the help!
                      • 3749
                      • 24,544 Posts
                      You should always use a return statement to display output from a snippet rather than echo or print, especially if you’re having trouble.

                      What happens if you do this:

                      $output = $modx->getChunk('myChunk',array(  
                        'name' => $http
                      ));
                      
                      return $output;
                        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