0

I cannot access the objects properties using the Twig dot notation. For example, from looking at the object dump, I should be able to do image.copyright, which should print "Blue button near the Cayman Islands, Caribbean (© Lawson Wood/Aurora Photos)" for the first item.

The error message I get is

Method "copyright" for object "SimpleXMLElement" does not exist in ARRaiDesignBundle:Default:wallpapers.html.twig at line 12

While dumping the object using dump(image) dumps each of the objects.

Controller class:

$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 file:

...
{% for image in xml %}
<p><pre>{{ image.copyright }}</pre></p>
{% endfor %}
...

Object dump using dump(image) in Twig:

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"
...

Can anyone suggest how to do this? I know I can use PHP rendering instead of Twig, but that's not a fix for me. Thanks.

4
  • Here is the line in the source that outputs that line: github.com/fabpot/Twig/blob/master/lib/Twig/Template.php#L424 Commented Mar 30, 2013 at 16:51
  • Have you tried casting $xml to an array? Commented Mar 30, 2013 at 17:31
  • @JaredFarrish I used the data you provided with simplexml_load_string($file). It seems symfony does not like bing's xml. As within <images> there is also <tooltips>. Which is a werid structure to iterate through. Although, standard php is fine, but twigs function is not perfect. Commented Mar 30, 2013 at 19:02
  • You should search for whether there's an issue, and report it if there's not one for this problem. Commented Mar 30, 2013 at 19:13

4 Answers 4

3

with example php doc http://www.php.net/manual/fr/simplexml.examples-basic.php
in twig, to get attribute 'type' from 'rating' item, use attributes (="@attributes") like :

{% for rating in movies.movie.rating %}
   {{ rating.attributes.type }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

1

It seems to me that your SimpleXMLElement does not have magic methods implemented. When you call in Twig object.property - it invokes getProperty() method of your object. Try do access property directly in Twig:

{{ image['copyright'] }}

5 Comments

I've tried that already, does not work: Key "copyright" in object (with ArrayAccess) of type "SimpleXMLElement" does not exist in ARRaiDesignBundle:Default:wallpapers.html.twig at line 12
Probably one of your objects really doesn't have such property? Have you tried to iterate it with default filter? {{ image['copyright']|default('') }}
@AmarjeetSinghRai otherwise you can try to dump each object from loop using twig and see what properties are accessible - {{ dump(image) }}
That then displays nothing. It does exist, look at the object dump above. I managed to display it perfectly without symfony.
that is how I got the object dump in twig. [Above]
1

This was happening due to Bing's werid XML structure, the last bit is not iterative friendly. With standard PHP it's fine, but with Twig, it doesn't catch the errors for the last element.

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

To fix this, I just unset tooltips. "unset($xml->tooltips)"

Thanks @JaredFarrish for providing clean xml. :)

Comments

0

I believe you have to proxy that object. Create a new object that wraps the SimpleXMLElement object and define a getCopyright() method.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.