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

simplexml function in php5
Goto page 1, 2  Next
 
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 -> General
View previous topic :: View next topic  
Author Message
smartymoon
Smarty Rookie


Joined: 29 Apr 2003
Posts: 15

PostPosted: Fri Jun 17, 2005 6:52 am    Post subject: simplexml function in php5 Reply with quote

I am testing simplexml function in php5.
i tested with the following files but no output at all.
please show me how to setup tpl file.

this example is from php manual.

thanks

s. moon

simple.xml
Code:

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Actor</actor>
   </character>
  </characters>
  <plot>
  a documentary.
  </plot>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
</movie>
</movies>
XML;
?>


test.php

Code:
<?php
include 'example.xml';
$xml = simplexml_load_string($xmlstr);
$smarty->assign('xml', $xml);
$smarty->display('test.tpl');
?>


test.tpl

Code:
{section name=x loop=$xml->movie}
  loop: {$smarty.section.x.loop}
  {$xml->movie[x]->title}
  {$xml->movie[x]->characters}
  {$xml->movie[x]->characters->character}
{section}
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Jun 17, 2005 7:14 am    Post subject: Reply with quote

hmmmm


Code:

{section name=x loop=$xml->movies}


instead of

Code:

{section name=x loop=$xml->movie}
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Jun 17, 2005 7:29 am    Post subject: Reply with quote

{section} doesn't work with simplexml. use {foreach}.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
smartymoon
Smarty Rookie


Joined: 29 Apr 2003
Posts: 15

PostPosted: Fri Jun 17, 2005 8:16 am    Post subject: Reply with quote

it seems messju is right.
section is not workable with simplexml.
returned value from simplexml is mixing of array and object.
it's too much confusing.
i'll try foreach but foreach has few features to use.

thanks

s. moon
Back to top
View user's profile Send private message
smartymoon
Smarty Rookie


Joined: 29 Apr 2003
Posts: 15

PostPosted: Fri Jun 17, 2005 8:22 am    Post subject: Reply with quote

// kills

using movies instead of movie has same result, no output

thanks

s. moon
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Jun 17, 2005 8:26 am    Post subject: Reply with quote

smartymoon wrote:
i'll try foreach but foreach has few features to use.


there is hardly anything you can do with {section} that you can't do also with {foreach}.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
smartymoon
Smarty Rookie


Joined: 29 Apr 2003
Posts: 15

PostPosted: Fri Jun 17, 2005 8:33 am    Post subject: Reply with quote

for your convenience, i'll post print_r dump of $xml
Code:

SimpleXMLElement Object
(
    [movie] => SimpleXMLElement Object
        (
            [title] => PHP: Behind the Parser
            [characters] => SimpleXMLElement Object
                (
                    [character] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [name] => Ms. Coder
                                    [actor] => Onlivia Actora
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [name] => Mr. Coder
                                    [actor] => El Actor
                                )

                        )

                )

            [plot] =>
  a documentary.
 
            [rating] => Array
                (
                    [0] => 7
                    [1] => 5
                )

        )

)
Back to top
View user's profile Send private message
smartymoon
Smarty Rookie


Joined: 29 Apr 2003
Posts: 15

PostPosted: Fri Jun 17, 2005 3:51 pm    Post subject: Reply with quote

Here is my solution for simplexml in php 5.
i tried with for but not worked, instead use foreach.

test.php
Code:

<?php
$smarty =& new Smarty;

include 'example.xml';

$xml = simplexml_load_string($xmlstr);

$tables = array();
foreach($xml->table as $t) { // don't use 'for'
  $table =  $t;
  $fields = array();
  foreach($table->field as $f) {
    $fields[] =  $f;
  }
  $type = (string)$table['type'];
  $tables[$type] = $fields;
}
$smarty->assign('tables', $tables);

$smarty->display('test.tpl');

?>


example.xml
Code:

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<data>
<table type="page">
<field><Name>page</Name><Template>home.htm</Template></field>
<field><Name>category</Name><Template>category.htm</Template></field>
</table>
<table type="company">
<field><Name>CNN</Name><Type>news</Type></field>
<field><Name>GM</Name><Type>car</Type></field>
</table>
</data>
XML;
?>


test.tpl
Code:

<!--[assign var=fields value=$tables.page]-->
<!--[section name=x loop=$fields]-->
id: <!--[$smarty.section.x.index+1]-->
Name: <!--[$fields[x]->Name]-->
Template: <!--[$fields[x]->Template]-->
<!--[/section]-->

<!--[assign var=fields value=$tables.company]-->
<!--[section name=x loop=$fields]-->
id: <!--[$smarty.section.x.index+1]-->
Name: <!--[$fields[x]->Name]-->
Type: <!--[$fields[x]->Type]-->
<!--[/section]-->


test result
Code:
id: 1
Name: page
Template: home.htm
id: 2
Name: category
Template: category.htm

id: 1
Name: CNN
Type: news
id: 2
Name: GM
Type: car
Back to top
View user's profile Send private message
timmerk
Smarty Regular


Joined: 06 Mar 2005
Posts: 36

PostPosted: Sat Jun 18, 2005 9:17 pm    Post subject: Reply with quote

Can we have smarty updated to work with the simplexml variables returned? Maybe we could pass it something saying its a simplexml variable, or it could parse them each time by checking what it is. THat might be slow though.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Sat Jun 18, 2005 9:23 pm    Post subject: Reply with quote

again:
- smarty works fine with simplexml
- {section} doesn't
- {foreach} does
- there is nothing (in this context) that can't be done with {foreach}

conclusion:
- there is no need to "enhance" smarty in that direction
Back to top
View user's profile Send private message Send e-mail Visit poster's website
timmerk
Smarty Regular


Joined: 06 Mar 2005
Posts: 36

PostPosted: Sat Jun 18, 2005 9:25 pm    Post subject: Reply with quote

I meant have section work with data returned from simplexml.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Sat Jun 18, 2005 9:32 pm    Post subject: Reply with quote

timmerk wrote:
I meant have section work with data returned from simplexml.


why?

section is merely a for()-loop. you normally don't use a for()-loop to iterate over children of a simpleXML-node in php-land, either.

you should use foreach() in php and you should use {foreach} in the template.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
timmerk
Smarty Regular


Joined: 06 Mar 2005
Posts: 36

PostPosted: Sat Jun 18, 2005 9:33 pm    Post subject: Reply with quote

Just because it would make smarty complete. Why well tell people that some functions can't be used with all types of data returned, but others can?
Also, I think section is a lot easier to use in some situations.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Jun 18, 2005 9:54 pm    Post subject: Reply with quote

timmerk wrote:
Just because it would make smarty complete. Why well tell people that some functions can't be used with all types of data returned, but others can?
Also, I think section is a lot easier to use in some situations.


I don't think it would make Smarty more complete nor do I think that it has something to do with what variable types functions can take -- there are many examples of needing to use specific types or structures in Smarty functions and this is no exception. In fact, even a normal associative array cannot be iterated with a section. Besides, section is typically harder to use than foreach and here it is actually impossible. If anything, section should be deprecated.
Back to top
View user's profile Send private message
timmerk
Smarty Regular


Joined: 06 Mar 2005
Posts: 36

PostPosted: Sat Jun 18, 2005 9:56 pm    Post subject: Reply with quote

About 80% of my clients use section. And in 3 PHP books I use, they prefer section.

So I doubt you should get rid of it.
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 -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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