Symfony如何在Twig上解析RSS feed

jmu*_*zco 5 php rss symfony twig

例如,在PHP中,解析RSS提要的方法可能是:

<?php
$rss = simplexml_load_file('http://blog.wordpress_site.com/feed/');

{{ rss }}

foreach ($rss->channel->item as $item) {
    echo $item->title;
    echo $item->link;
    echo $item->description;
    echo $item->guid;
}
?>
Run Code Online (Sandbox Code Playgroud)

我怎么能在Twig上有这个?

更新:感谢回复我得到了它.现在它逐项获取,但不是像帖子的图像,类别或文本等字段:

SimpleXMLElement {#955 ?
  +"title": "Website. Description of the website"
  +"link": "http://blog.website.com/liktothepost"
  +"pubDate": "Fri, 17 Feb 2017 07:56:43 +0000"
  +"category": SimpleXMLElement {#1131}
  +"guid": "http://blog.website.com/?p=400"
  +"description": SimpleXMLElement {#953}
}
Run Code Online (Sandbox Code Playgroud)

Alv*_*unk 2

您将创建一个带有操作的控制器,将对象传递到您想要渲染的 Twig 文件,如下所示:

public function viewRSSAction(Request $request){
    $rss = simplexml_load_file('http://blog.wordpress_site.com/feed/');

    return $this->render('my_rss.html.twig', array(
            'rss' => $rss,
    ));
}
Run Code Online (Sandbox Code Playgroud)

那么你的my_rss.html.twig可能看起来像这样:

{% for item in rss %}
    {{ item.title }}
    {{ item.link }}
    {{ item.description }}
    {{ item.guid }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)