We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33982 ☆ A M B ☆
    • 476 Posts
    Version and environment: Running MODX 2.2.6-pl on Amazon's server.

    Pretty embarrassed to admit it, but I've only actually built my own PHP snippets a few times in the last several years of using MODX (there I said it). As such, I have an some existing PHP snippet that's not in MODX that works great on my server on a static PHP page to grab info from an RSS feed. It's basically pulling in info from an RSS feed and I have no idea how to convert it into a MODX snippet and integrate into my MODX template. If you look at the code, it's pretty straightforward. The first part is just grabbing the function itself, pulling the values from the RSS feed and storing the variables in an array and then at the bottom we repeat the chunk of HTML 4 times plugging in the variables extracted from the RSS feed stored in the array and on the last of the 4 outputs, putting in a "last" class. I just need help converting this into a MODX snippet.

    <?php
    include('assets/rss-feed/function_getNewsData.php');
    $articles = array();
    $articles = getNewsBlock('http://www.website-url.net/admin/rss/fitness.rss', 4);
    ?>
    
    <?php foreach ($articles as $article) { ?>
    	<div class="latest-news-post <?php if ($article === end($articles)) echo "last"; ?>">
    		<div class="img-arrow-hover">				
    	
    			<a href="<?php echo $article["link"]; ?>" class="latest-news-post-link"><img class="latest-news-post-img" src="<?php echo $article["image"]; ?>" alt="<?php echo $article["title"]; ?>" width="288" height="118"></a>
    			
    			<img class="latest-news-post-arrow" src="assets/template/images/news/news-arrow.png" alt="news-arrow" width="288" height="10">
    			
    		</div><!-- .img-arrow-hover -->						
    		
    		<img src="assets/template/images/news/news-arrow-shadow.png" alt="news-arrow-shadow" width="288" height="13">
    	
    		<h4><a href="<?php echo $article["link"]; ?>"><?php echo $article["title"]; ?></a></h4>
    		
    		<p><?php echo $article["content"]; ?></p>
    		
    	</div><!-- .latest-news-post -->
    <?php } ?>
    


    Here was my attempt. I created a snippet named "getFeedDev".

    include('assets/rss-feed/function_getNewsData.php');
    $articles = array();
    $articles = getNewsBlock('http://www.website-url.net/admin/rss/fitness.rss', 4);
    $title = $article["title"];
    $output = $modx->getChunk('rss-feed',array(
      'title' => $title,
    ));
    return $output;
    


    Then I created this chunk named "rss-feed" to simply output the title but I get nothing:

    <p>Hello [[+title]]</p>
      Precision Web Development ... SmashStack.com
      • 10487 MODX Staff
      • 1,535 Posts
      On first glance, you're missing the foreach() loop in your snippet. $article is never being set. Also, are you calling your snippet uncached?
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        In your foreach loop, instead of generating the output, you'll want to set the placeholders.

        Both getResources and Wayfinder generate an array of the name/value pairs from the results, then instantiate a new $chunk object for the tpl and use the $chunk->process($placeholder_array, $chunk_name) to insert the values into the appropriately named placeholders in the tpl.
          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
          • 4172
          • 5,888 Posts
          your snippet should look like that:

          <?php
          
          $outputSeparator = $modx->getOption('outputSeparator',$scriptProperties,'');
          
          include ('assets/rss-feed/function_getNewsData.php');
          $articles = array();
          $articles = getNewsBlock('http://www.website-url.net/admin/rss/fitness.rss', 4);
          
          foreach ($articles as $article) {
              $article['_last'] = $article === end($articles) ? 'last' : '';
              $output[] = $modx->getChunk('rss-feed', $article);
          }
          
          return implode($outputSeparator,$output);
          


          your chunk like that:

              <div class="latest-news-post [[+_last]]">
                  <div class="img-arrow-hover">            
               
                      <a href="[[+link]]>" class="latest-news-post-link">
                      <img class="latest-news-post-img" src="[[+image]]" alt="[[+title]]>" width="288" height="118"/>
                      </a>
                       
                      <img class="latest-news-post-arrow" src="assets/template/images/news/news-arrow.png" alt="news-arrow" width="288" height="10"/>
                       
                  </div><!-- .img-arrow-hover -->                    
                   
                  <img src="assets/template/images/news/news-arrow-shadow.png" alt="news-arrow-shadow" width="288" height="13"/>
               
                  <h4><a href="[[+link]]">[[+title]]</a></h4>
                   
                  <p>[[+content]]</p>
                   
              </div><!-- .latest-news-post -->
          
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 33982 ☆ A M B ☆
            • 476 Posts
            @Bruno17 ... thanks so much! That worked perfectly. Probably would have taken me forever to figure out, but that did the trick. I owe you a beer!
              Precision Web Development ... SmashStack.com