2013-09-28 66 views
0

我有這樣的一個xml:使用如何訪問Twig(Symfony)中特定節點的XML屬性?

item.domain.currentPrice 

<item> 
    <domain> 
    <currentPrice currency="EUR">17.9</currentPrice> 
    </domain> 
</item> 

我可以呈現在嫩枝17.9的價值,但我怎麼能訪問屬性「貨幣」來呈現呢?

item.domain.currentPrice['currency'] 

不起作用。然後我得到:

Key "curreny" in object (with ArrayAccess) of type "SimpleXMLElement" does not exist 

誰能幫我?

回答

2

您可以訪問通過屬性的貨幣屬性值()方法:

{{ xml.domain.currentPrice.0.attributes.currency }} 

,你會做,在PHP:

$xml->domain->currentPrice[0]->attributes()->currency; 

其實枝杈使得從樹枝代碼到PHP這種轉變代碼和php代碼最終執行。

您可以仔細檢查SimpleXMLElement的API以瞭解您可以使用哪些方法和屬性。