Pop\Feed\Reader::render PHP Method

render() public method

Method to render the feed
public render ( boolean $ret = false ) : mixed
$ret boolean
return mixed
    public function render($ret = false)
    {
        if (null === $this->template) {
            throw new Exception('Error: The feed item template is not set.');
        }
        $feed = $this->adapter()->getFeed();
        if (!isset($feed['items'])) {
            throw new Exception('Error: The feed currently has no content.');
        }
        $output = null;
        // Loop through the items, formatting them into the template as needed, using the proper date format if appropriate.
        foreach ($feed['items'] as $item) {
            $tmpl = $this->template;
            foreach ($item as $key => $value) {
                if (strpos($tmpl, '[{' . $key . '}]') !== false) {
                    if (null !== $this->dateFormat && (stripos($key, 'date') !== false || stripos($key, 'published') !== false)) {
                        $value = date($this->dateFormat, strtotime($value));
                    }
                    $tmpl = str_replace('[{' . $key . '}]', $value, $tmpl);
                }
            }
            $output .= $tmpl;
        }
        // Return the final output.
        if ($ret) {
            return $output;
        } else {
            echo $output;
        }
    }

Usage Example

Esempio n. 1
0
    public function testRssRender()
    {
        $tmpl = <<<NEWS
    <div class="news-div">
        <a href="[{link}]">[{title}]</a><br />
        <strong>[{published}]</strong> ([{time}])<br />
        <p>[{description}]</p>
    </div>

NEWS;
        $feed = new Feed\Reader(new Feed\Format\Rss('http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=h&output=rss', 4));
        $feed->setTemplate($tmpl);
        $this->assertContains('Google', $feed->title);
        $code = $feed->render(true);
        ob_start();
        $feed->render();
        $output = ob_get_clean();
        ob_start();
        echo $feed;
        $echoOutput = ob_get_clean();
        $this->assertContains('<div class="news-div">', $code);
        $this->assertContains('<div class="news-div">', $output);
        $this->assertContains('<div class="news-div">', $echoOutput);
    }