【发布时间】:2013-03-21 03:43:53
【问题描述】:
我无法使用 Twig 点表示法访问对象属性。例如,通过查看对象转储,我应该能够执行 image.copyright,它应该为第一项打印“加勒比开曼群岛附近的蓝色按钮 (© Lawson Wood/Aurora Photos)”。
我得到的错误信息是
第 12 行的 ARRaiDesignBundle:Default:wallpapers.html.twig 中不存在对象“SimpleXMLElement”的方法“版权”
使用 dump(image) 转储对象时,会转储每个对象。
控制器类:
$host = 'http://www.bing.com';
$file = $host . '/HPImageArchive.aspx?format=xml&idx=0&n=10&mkt=en-US';
$xml = simplexml_load_file($file);
return $this->render('ARRaiDesignBundle:Default:wallpapers.html.twig', array('xml' => $xml, 'host' => $host));
wallpapers.html.twig 文件:
...
{% for image in xml %}
<p><pre>{{ image.copyright }}</pre></p>
{% endfor %}
...
在 Twig 中使用 dump(image) 转储对象:
object(SimpleXMLElement)#268 (12) {
["startdate"]=>
string(8) "20130330"
["fullstartdate"]=>
string(12) "201303300000"
["enddate"]=>
string(8) "20130331"
["url"]=>
string(46) "/az/hprichbg/rb/BlueButton_EN-US1108621411.jpg"
["urlBase"]=>
string(43) "/az/hprichbg/rb/BlueButton_EN-US10208337365"
["copyright"]=>
string(77) "Blue button near the Cayman Islands, Caribbean (© Lawson Wood/Aurora Photos)"
["copyrightlink"]=>
string(74) "http://www.bing.com/search?q=Blue+Button+%28Porpita+porpita%29&form=hpcapt"
["drk"]=>
string(1) "1"
["top"]=>
string(1) "1"
["bot"]=>
string(1) "1"
...
谁能建议如何做到这一点?我知道我可以使用 PHP 渲染而不是 Twig,但这对我来说不是解决办法。谢谢。
【问题讨论】:
-
@JaredFarrish 我使用了您通过 simplexml_load_string($file) 提供的数据。 symfony 似乎不喜欢 bing 的 xml。在
中也有 。这是一个奇怪的结构来迭代。虽然,标准的php很好,但是twigs的功能并不完善。 -
你应该搜索是否有问题,如果没有这个问题,请报告。