|
Posted by "messju mohr" on 06/03/05 20:32
On Sat, Jun 04, 2005 at 01:50:35AM +0900, dc wrote:
> when is an array not an array? when its simpleXML
this should read:
Q: when is an array not an array?
A: when its simpleXML
(just a joke, sorry).
honestly: i think simple-xml looks pretty slick at first sight, but
totally brain-damaged (if not -dead) at second sight. but that
personal impression may just be by me, because i'm not bright enough
to get it properly.
in simple_xml all your nodes are objects, strings and arrays at the
same time. they just behave differently on the context they are used
in.
forget about {section}, it doesn't work on them, but {foreach} works
fine since Smarty-2.6.8.
example.
curl -O http://slashdot.org/slashdot.xml
to get an xml-file for testing:
<?xml version="1.0"?>
<backslash xmlns:backslash="http://slashdot.org/backslash.dtd">
<story>
<title>AOL Open Sourcing Audio &amp; Video Technology</title
>
<url>http://slashdot.org/article.pl?sid=05/06/03/1529240</url>
<time>2005-06-03 16:48:00</time>
<author>Zonk</author>
<department>giving-back-to-america</department>
<topic>120</topic>
<comments>0</comments>
<section>mainpage</section>
<image>topicaol.gif</image>
</story>
<story>
<title>Will Next-Gen Consoles Kill Off PC Gaming?</title>
<url>http://slashdot.org/article.pl?sid=05/06/03/1510223</url>
<time>2005-06-03 16:19:00</time>
<author>Zonk</author>
<department>death-knell</department>
<topic>206</topic>
<comments>196</comments>
<section>games</section>
<image>topicgamespcgames.gif</image>
</story>
.....
</backslash>
then in php:
$backslash = simplexml_load_file(BASEDIR . 'simplexml/slashdot.xml');
$this->assign('backslash', $backslash);
and in the template:
{foreach from=$backslash->story item=story}
{$story->title}
{$story->url}
{/foreach}
gives me:
AOL Open Sourcing Audio & Video Technology
http://slashdot.org/article.pl?sid=05/06/03/1529240
Will Next-Gen Consoles Kill Off PC Gaming?
http://slashdot.org/article.pl?sid=05/06/03/1510223
....
if you want to access child-elements of a node use {$node->child},
if you want to access attributes of a node use {$node.attrname},
if you want to dump the contents of a node use {$node}
i don't think that smarty is *that* bad on simple_xml, but i must
admit i don't know anybody that uses this combo in production.
greetings
messju
> it seems simpleXML is hard to work with smarty.
>
> this (smarty tpl) works:
>
> <li>{$xml->example[0]->title}
> <li>{$xml->example[1]->title}
>
> gives me a nice list of titles.
>
> a smarty section gives nothing (it doesnt loop)
> {section name=id loop=$xml->example }
>
> but a foreach *does* loop thru,
> {foreach from=$xml->example item=ex }
>
> but the item coming thru is the first leaf of the XML.
>
> the only way seems to be to have two foreach loops. however, for each
> cannot skip
> (start=1) the first index (xml header stuff), so an if statement is needed.
>
> -------
> final kludge: (works)
>
> {foreach from=$xml item=list name='outer' }
> {if $smarty.foreach.outer.iteration>1}
> {foreach from=$list item=ex name='loop' }
>
>
> unfortunately this code is to be shown onscreen at a seminar, which is a
> bit embarassing really.
>
> /dc
>
> On Sat, 04 Jun 2005 00:45:09 +0900, Monte Ohrt <monte@ohrt.com> wrote:
>
> >If it is assigned as an object, then you must access in Smarty like such:
> >
> >{$ex->event}
> >
> >dc wrote:
> >
> >>thanks for the amazingly quick response...
> >>
> >>>If you're only getting the first letter, then your data structure may
> >>>not be what you are expecting.
> >>>
> >>>What is the output of
> >>>
> >>>print_r($xml);
> >>
> >>
> >>
> >>SimpleXMLElement Object
> >>(
> >>[event] => Macromedia ?$B=8Cf%<%_
> >>[example] => Array
> >>(
> >>[0] => SimpleXMLElement Object
> >>(
> >>[title] => ?$B;M3Q!\%F%-%9%H
> >>[src] => demo01.php
> >>[description] => ?$B;M3Q!\%F%-%9%H!#F|K\8lBP1~
> >>)
> >>
> >>[1] => SimpleXMLElement Object
> >>(
> >>[title] => ?$B30ItSWF?$B%U%!%$%k
> >>[src] => demo02.php
> >>[description] => SWF?$B8F$S=P$9
> >>)
> >>
> >>)
> >>)
> >>
> >>
> >>do i have to convert the SimpleXMLElement Objects to simple arrays for
> >>smarty?
> >>
> >>/dc
> >>
> >>
> >>On Sat, 04 Jun 2005 00:33:51 +0900, Monte Ohrt <monte@ohrt.com> wrote:
> >>
> >>>If you're only getting the first letter, then your data structure may
> >>>not be what you are expecting.
> >>>
> >>>What is the output of
> >>>
> >>>print_r($xml);
> >>>
> >>>?
> >>>
> >>>dc wrote:
> >>>
> >>>>does smarty play nicely with objects created by PHP's simple xml?
> >>>>
> >>>>---------------------- PHP
> >>>>$xml = simplexml_load_file('assets/exList.xml');
> >>>>$sm->assign("xml", $xml);
> >>>>
> >>>>---------------------- XML
> >>>><exampleList>
> >>>>
> >>>><event>?$B=8Cf%<%_</event>
> >>>>
> >>>><example>
> >>>><title>?$B;M3Q!\%F%-%9%H</title>
> >>>><src>demo01.php</src>
> >>>><description>?$B;M3Q!\%F%-%9%H!#F|K\8lBP1~</description>
> >>>></example>
> >>>>
> >>>>etc
> >>>>
> >>>>---------------------- smarty template:
> >>>>{foreach from=$xml item=ex }
> >>>>{$ex.event}
> >>>>{$ex.title}
> >>>>{/foreach}
> >>>>
> >>>>
> >>>>but this only gives me the first letter of the item...
> >>>>
> >>>>would the toplevel of the XML tree be passed into smarty?
> >>>>eg exampleList/event
> >>>>
> >>>>exampleList/example [0]? first item?
> >>>>
> >>>>
> >>>>thanks for any help. would have thot this would be on the smarty site
> >>>>as XML display with smarty would seem to be a major use...
> >>>>
> >>>
> >>
> >
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
[Back to original message]
|