【问题标题】:Unable to access object attribute values using Twig in Symfony 2.2无法在 Symfony 2.2 中使用 Twig 访问对象属性值
【发布时间】: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,但这对我来说不是解决办法。谢谢。

【问题讨论】:

  • 你试过casting $xml to an array吗?
  • @JaredFarrish 我使用了您通过 simplexml_load_string($file) 提供的数据。 symfony 似乎不喜欢 bing 的 xml。在 中也有 。这是一个奇怪的结构来迭代。虽然,标准的php很好,但是twigs的功能并不完善。
  • 你应该搜索是否有问题,如果没有这个问题,请报告。

标签: php oop symfony web twig


【解决方案1】:

带有示例 php 文档 http://www.php.net/manual/fr/simplexml.examples-basic.php
在 twig 中,要从 'rating' 项目中获取属性 'type',请使用属性 (="@attributes"),例如:

{% for rating in movies.movie.rating %}
   {{ rating.attributes.type }}
{% endfor %}

【讨论】:

    【解决方案2】:

    在我看来,您的SimpleXMLElement 没有实现魔法方法。当您调用 Twig object.property 时 - 它会调用您的 objectgetProperty() 方法。尝试直接在 Twig 中访问属性:

    {{ image['copyright'] }}
    

    【讨论】:

    • 我已经尝试过了,但不起作用:第 12 行的 ARRaiDesignBundle:Default:wallpapers.html.twig 类型“SimpleXMLElement”的对象(使用 ArrayAccess)中的键“版权”不存在
    • 可能你的一个对象真的没有这样的属性?您是否尝试过使用default 过滤器对其进行迭代? {{ image['copyright']|default('') }}
    • @AmarjeetSinghRai 否则您可以尝试使用 twig 从循环中转储每个对象并查看可以访问哪些属性 - {{ dump(image) }}
    • 然后什么都不显示。它确实存在,请查看上面的对象转储。我设法在没有 symfony 的情况下完美地显示它。
    • 这就是我在树枝中获得对象转储的方式。 [以上]
    【解决方案3】:

    这是由于 Bing 的奇怪 XML 结构而发生的,最后一点对迭代不友好。使用标准 PHP 没问题,但使用 Twig,它不会捕获最后一个元素的错误。

    <images>
    <image>...</image>
    <image>...</image>
    <image>...</image>
    <image>...</image>
    <image>...</image>
    <image>...</image>
    <image>...</image>
    <image>...</image>
    <tooltips>...</tooltips>
    </images>
    

    为了解决这个问题,我只是取消了工具提示。 “未设置($xml->工具提示)”

    感谢 @JaredFarrish 提供干净的 xml。 :)

    【讨论】:

      【解决方案4】:

      我相信您必须代理该对象。创建一个包装 SimpleXMLElement 对象的新对象并定义一个 getCopyright() 方法。

      class Element 
      {
          protected $xmlElement;
          public function getCopyright() 
          {
              return $this->xmlElement->copyright;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多