Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

BUG: XML-Objekt -> Array -> XML-Objekt wandelt das obj

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Sun Jan 30, 2005 12:54 am    Post subject: BUG: XML-Objekt -> Array -> XML-Objekt wandelt das obj Reply with quote

Hallo Smarty Gemeinde

Ich habe probleme mit den SimpleXML Objekt in Smarty.

Er macht aus ein Array mit Objekt nur ein einzelnes Array im Compile.

PHP:
Code:
$smarty->assign('xmlObj',$SimpleXMLobj);


Smarty:
Das geht:
Code:
{$xmlObj->message[0]}


Das geht nicht, ergebnis ist in item ist nicht das objekt.
Code:
{foreach from=$xmlObj->item[0] item=item}
         {$item->number}<br />
{/foreach}



Smarty Debug:
Code:
{$xmlObj}
SimpleXMLElement Object (8)
GUID => 4icdukc6lvbpgjc2ghupghvjb0-1
processtype => searchcatalog
processstatus => 0
    unitprice => 17,1
    recommendedretailprice => 34
    description1 => GASGRIFF 270.9.9LS/KSZ 2
    description2 => 101550
    availablestatus => 1
    infourl => http://xml.bike-parts.de/katalogtexte...
    pictureurl => http://xml.bike-parts.de/images/51015...
    ean => 4032191102184
  1 => SimpleXMLElement Object (9)
    number => 5101531700
    unitprice => 17,8
    recommendedretailprice => 35,9
    description1 => GASGRIFF 270.11.9 LS/KSZ/KSZ2
    description2 => 101 590
    availablestatus => 1
    infourl => http://xml.bike-parts.de/katalogtexte...
    pictureurl => http://xml.bike-parts.de/images/51015...
    ean => 4032191102214
  2 => SimpleXMLElement Object (8)
   .......
    .......
  25 => SimpleXMLElement Object (9)
{$ofpages} 2
{$page} 1
{$pagesize} 25


Compile Ergenins von Smarty:
Code:
<?php echo $this->_tpl_vars['xmlObj']->message[0]; ?>
<?php if (count($_from = (array)$this->_tpl_vars['xmlObj']->item[0])):
    foreach ($_from as $this->_tpl_vars['items']):
?>
<?php echo $this->_tpl_vars['items']->number; ?>


Was mach ich falsch?

Der Compile Code müsste doch so in der Richzung aussehen:
Code:
foreach ( $this->_tpl_vars['xmlObj']->item AS $item)
        echo $item->number;



Für jede Hilfe dankbar!


gruß Alex


Last edited by doc_12 on Sun Jan 30, 2005 11:49 pm; edited 2 times in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Jan 30, 2005 9:29 am    Post subject: Reply with quote

smarty macht eher sowas wie:
[php:1:db698c6231]
foreach ((array)$this->_tpl_vars['objItems']->item AS $item)
echo $item->number;

[/php:1:db698c6231]

das from-attribute wird ein array convertiert.

ich denke das ist das problem.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Sun Jan 30, 2005 12:47 pm    Post subject: Reply with quote

Hallo messju

Ja das stimmt.

Was kann ich machen das er das nicht mehr tut ???
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jan 31, 2005 8:25 am    Post subject: Reply with quote

doc_12 wrote:
Was kann ich machen das er das nicht mehr tut ???


statt {foreach} eine eigene block-function schreiben, oder den compiler patchen.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Mon Jan 31, 2005 8:29 am    Post subject: Reply with quote

Hallo messju

Kannst du mir auf die Sprünge helfen

Bei der Blockfunfunktion habe ich das Problem mit der ersetzung des Contents, sprich {$number} usw.

Gibt es da auch ein fertiges plugin in dem ich key und value übergebe und der mir das ersetzt?


gruß Alex
Back to top
View user's profile Send private message
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Mon Jan 31, 2005 3:44 pm    Post subject: Block Function Reply with quote

Block Function Problem

TPL:
Code:
  {Loop_XML from=$objXML->item assign=item }
    <tr><td>
       Der Content<br />{$item.number}   
    </td></tr>
  {/Loop_XML}


PLUGIN:
Code:

function smarty_block_Loop_XML($params, $content, &$smarty, &$repeat) {
    if (!isset($params['from'])) {
        $smarty->trigger_error("foreach_xml: missing 'from' parameter");
        return;
    }
    var_dump($params['from'])
    return $content . "Anzahl: ".count($params['from']) ." von ". $params['from'];
   .......
   .....
}


Ausgabe von VAR_DUMP ist:
2 Objekte und zwar 2 mal das selbe Objekt obwohl es 20 sein müssten.

siehe 1 Post das $objXML hat 20 items.

Kann mir keiner helfen denn das ist zu 99% ein BUG

Für jede Hilfe dankbar:

gruß Alex


System:
Linux / PHP Version 5.0.3 / Apache2 (mod_php)
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jan 31, 2005 3:51 pm    Post subject: Re: Block Function Reply with quote

doc_12 wrote:
Kann mir keiner helfen denn das ist zu 99% ein BUG


ich packe nochmal 90% drauf, dass es ein "feature" von simplexml ist Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Mon Jan 31, 2005 10:11 pm    Post subject: Re: Block Function Reply with quote

Quote:
ich packe nochmal 90% drauf, dass es ein "feature" von simplexml ist Smile


Bist du dir da sicher?

Den in PHP selber geht alles!!

Wenn das wirklich so ist zeige mir doch eine Alternative BITTE!
Ich brauche Hilfe und keine Qutes (sorry)

Ich bin aber bald am verzweifeln und wenn das so weiter geht lass ich das mit Smarty und nehme XSLT.
Ich weiß dass ich erst 3 Tage ernsthaft mit Smarty arbeite, aber das mit den Objekten ist schon nervig!

Und ich brauche ein paar Lösungen oder Vorschläge dies anders zu Lösen.
Denn ich bin der Meinung das Smarty ein Problem damit hat und nicht SimpleXML!

Ich hoffe ich bekomme von dir noch eine Antwort!


Gruß Alex

PS: sind wir 2 die einzigen hier, oder habe ich etwas ganz exotisches
Back to top
View user's profile Send private message
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Tue Feb 01, 2005 8:31 am    Post subject: Stand der Dinge Reply with quote

Das Geht richtig !!
PHP
Code:

          $result = $objXML->xpath('/root/item');
          $smarty->assign_by_ref('objXML',$objXML);


TPL:
Code:
 
{foreach from=$objItems item=item }
       {$item->number}   
  {/foreach}

Ist aber nicht toll denn ich brauch auch noch die anderen Elemente vor und nach item.
Das würde bedeuten das ich zweimal das XML Element anhängen muss und für jeden weiteren Loop noch einmal und dazu muss der Code immer angepasst werden. und genau da sollte ja der Vorteil liegen mit den XML Dokument das ich es einfach erweitern kann und im Template die neue Ausgabe einfüge.

-------------------------------------------

Das geht nicht Mad
PHP
Code:

          $result = $objXML->xpath('/root');
          $smarty->assign_by_ref('objXML',$objXML);


TPL:
Code:
 
{foreach from=$objItems->item item=item }
       {$item->number}   
  {/foreach}


Woran liegt es jetzt: Smarty, SimpleXML oder ich selbst ?

Können wir das Prob jetzt mal analysieren ?

Gruß Alex
Back to top
View user's profile Send private message
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Tue Feb 01, 2005 8:51 am    Post subject: Smarty BUG Reply with quote

Es ist ein BUG in Smarty:
ab Line 1174
Code:

            $output .= "        {$foreach_props}['last']  = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n";
        } else {
            // $output .= "if (count(\$_from = (array)$from)):\n";       
            $output .= "if (count(\$_from = $from)):\n";
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
        }
        $output .= '?>';


Habe die Typ-Umwandlung rausgenommen, da müsste eine abfrage hin ob es ein Objekt ist !

Wo kann man diesen BUG in Deutsch melden?

Gruß Alex
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Feb 01, 2005 8:58 am    Post subject: Re: Stand der Dinge Reply with quote

in meiner ersten antwort habe ich schon gesagt: das problem ist das (array)-cast in dem {foreach}.

da dir diese info anscheinend nicht ausgereicht hat:
das (array)-cast wird in Smarty_Compiler.class.php in der funktion _compiler_foreach_start() erzeugt. in Smarty-2.6.6 in zeile 1167 (für foreaches mit name-attribute) und in zeile 1176 (für foreaches ohne name-attribute).

wenn du SimpleXML mit smarty verwenden willst musst du diese beiden Zeilen wohl anpassen. ich weiss, dass das doof ist, da in den internals rumzufummeln, aber eine lösung in einer Smarty-2.6.x-release ist nicht in sich.

doc_12 wrote:
Können wir das Prob jetzt mal analysieren ?

nee, ich hab keinen bock mehr.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Tue Feb 01, 2005 3:38 pm    Post subject: Reply with quote

Hi doc,
wieso willst du denn unbedingt ein Objekt wenn im Temlate sowieso nur dessen Eigenschaften ausgegeben werden. Ein (array) cast genügt dazu voll auf. Insbesondere wenn die Seite gecached wird.
Ansonsten probiers mit register_object(), vorausgesetzt SimpleXML (was ich nicht kenne) hat Methoden um seine eigenen Inhalte/Eigenschaften auszugeben.

Ach ja: mit deinem fordernden und harschem Ton brauchst du dich nicht wundern, wenn die Freiwilligen ihre Hilfe und Erklärungen einstellen Wink

Viel Spaß,
CirTap
Back to top
View user's profile Send private message
doc_12
Smarty Rookie


Joined: 04 Jan 2005
Posts: 13

PostPosted: Wed Feb 02, 2005 2:51 pm    Post subject: Reply with quote

Erst mal danke ihr zwei

Wenn mein Ton so schlimm war dann Entschuldige ich mich.

Lassen wir das jetzt mal mit den Ton usw. sonst gibt es eine endlose Diskusion.

Quote:
wieso willst du denn unbedingt ein Objekt wenn im Temlate sowieso nur dessen Eigenschaften ausgegeben werden. .


Wenn das XML erweitert umgestellt oder sonstiges brauche ich das nur im Template zu ändern, sprich auf das objekt zugreifen ohne das ich am PHP Code ändern muss.

Quote:
Ein (array) cast genügt dazu voll auf. Insbesondere wenn die Seite gecached wird

Verstehe nicht ganz was du meinst.
Wenn du eine Typumwandlung von Simple to array meinst das geht nicht.
Hier geht nur ein XML to Array.

Quote:
Ansonsten probiers mit register_object(), vorausgesetzt SimpleXML (was ich nicht kenne) hat Methoden um seine eigenen Inhalte/Eigenschaften auszugeben.

Geht LEIDER nicht.


Gruß Alex
Back to top
View user's profile Send private message
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Wed Feb 02, 2005 3:49 pm    Post subject: Reply with quote

Hi,
Quote:
Wenn du eine Typumwandlung von Simple to array meinst das geht nicht.

doch Smile Frage ist nur, ob das dann in deinem Template zu gebrauchen ist:
[php:1:ec55e03d0f]$XMLobject =& new SimpleXML($xmlfile); // oder wie auch immer
...
$smarty->assign("XMLdata", (array)$XMLobject);[/php:1:ec55e03d0f]
Standard PHP. Damit wird das Objekt $XMLobject in ein quasi "statisches" assoziatives Array "konvertiert" welches dann auch als solches im Template verwendet werden kann:
Code:
{$XMLdata.XML.root.value}
oder wie auch immer dieses Objekt aufgebaut ist - ich kenns ja nicht.
Schau mal im Kapitel 6 des PHP Handbuch nach: Types - Type juggling

Quote:
Hier geht nur ein XML to Array.

na dann eben dieses Array, wenn es "nur" um den Inhalt der XML-Datei geht. Wenn du die Datenstruktur (wie auch immer sie aussieht) als assoziatives Array im Template hast, kannst du ja mit foreach/section und Co. praktisch alles damit machen. Und wenn sich die XML-Datei ändern, Elementnamen etc., dann bleibt der PHP Code ja unverändert.

Viel Spaß
CirTap
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP