We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36607 ☆ A M B ☆
    • 44 Posts
    Hi everyone

    Anyone able to help with the GetFeed snippet?

    My queries:

    1. Is it possible to truncate the [[+description]] placeholder without using an output modifier? I have tried an output modifier but for some reason it messes up my site!

    2. Is it possible to exclude certain attributes of a feed ie certain css classes and the contents of the class?

    3. Is it possible to exclude certain html elements of the feed?

    Matt
      • 36607 ☆ A M B ☆
      • 44 Posts
      Hi everyone

      I’m no php developer and my php skills are rudimentary, but with the help of a friend I managed to get my problem resolved.

      My main problem with the getfeed snippet was with using an output filter with a placeholder within my &tpl chunk. The site that I am developing is essentially a sector specific rss aggregator similar to Alltop and Popurls.

      I wanted to display in a tooltip a short description of the main rss description, limited to 150 characters. The tooltip is a hidden element that becomes displayed when hovering over the "title" element.

      In my chunk I therefore had placeholders for the link and title content of the rss feed:
      [[+link]] [[+title]]
      and a placeholder for the description, which was combined with an output filter:
      [[+description:limit=`150`]]


      For some reason though the [[+description]] placeholder messed up my site and wouldn’t allow me to have more than two getfeed calls. In addition, the "limit" output filter didn’t work consistently.

      With some help from a friend though we managed to slightly amend the getfeed snippet to do what I needed. Essentially, we stripped out the html, limited the description to 150 characters and added an elipsis. The slight amendments to the snippet, which work for me, are set out below. Let me know though if any of you guys spot something dangerous!:

      if (!defined('MAGPIE_OUTPUT_ENCODING')) {
          $outputEncoding = $modx->getOption('outputEncoding',$scriptProperties,'UTF-8');
          define('MAGPIE_OUTPUT_ENCODING',$outputEncoding);
      }
      $chars = isset($chars) ? (integer) $chars : 150;
      $limit = isset($limit) ? (integer) $limit : 0;
      $offset = isset($offset) ? (integer) $offset : 0;
      $totalVar = !empty($totalVar) ? $totalVar : 'total';
      $total = 0;
      $output = array();
      if (!empty($url) && $modx->getService('rss', 'xmlrss.modRSSParser')) {
          $rss = $modx->rss->parse($url);
          if (!empty($rss) && isset($rss->items)) {
              $total = count($rss->items);
              $modx->setPlaceholder($totalVar, $total);
              $itemIdx = 0;
              $idx = 0;
              while (list($itemKey, $item) = each($rss->items)) {
                  if ($idx >= $offset) {
                      if (!empty($tpl)) {
                          $clip = substr(strip_tags($item['description']), 0, $chars);
                          $clip .= (strlen(strip_tags($item['description']))>strlen($clip)) ? '...' : '';
                          $item['description'] = $clip;
                          $output[] = $modx->getChunk($tpl, $item);
                      } else {
                          $output[] = '<pre>'.$idx.': ' . print_r($item, true) . '</pre>';
                      }
                      $itemIdx++;
                      if ($limit > 0 && $itemIdx+1 > $limit) break;
                  }
                  $idx++;
              }
          } else {
              $modx->log(modX::LOG_LEVEL_ERROR, "Error parsing RSS feed at {$url}", '', 'getFeed', __FILE__, __LINE__);
          }
      }
      $output = implode("\n", $output);
      
      if (!empty($scriptProperties['toPlaceholder'])) {
          $modx->setPlaceholder($scriptProperties['toPlaceholder'],$output);
          return '';
      }
      return $output;