View previous topic :: View next topic |
Author |
Message |
smartymoon Smarty Rookie
Joined: 29 Apr 2003 Posts: 15
|
Posted: Fri Jun 17, 2005 6:52 am Post subject: simplexml function in php5 |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Fri Jun 17, 2005 7:14 am Post subject: |
|
|
hmmmm
Code: |
{section name=x loop=$xml->movies}
|
instead of
Code: |
{section name=x loop=$xml->movie}
|
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Jun 17, 2005 7:29 am Post subject: |
|
|
{section} doesn't work with simplexml. use {foreach}. |
|
Back to top |
|
smartymoon Smarty Rookie
Joined: 29 Apr 2003 Posts: 15
|
Posted: Fri Jun 17, 2005 8:16 am Post subject: |
|
|
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 |
|
smartymoon Smarty Rookie
Joined: 29 Apr 2003 Posts: 15
|
Posted: Fri Jun 17, 2005 8:22 am Post subject: |
|
|
// kills
using movies instead of movie has same result, no output
thanks
s. moon |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Jun 17, 2005 8:26 am Post subject: |
|
|
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 |
|
smartymoon Smarty Rookie
Joined: 29 Apr 2003 Posts: 15
|
Posted: Fri Jun 17, 2005 8:33 am Post subject: |
|
|
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 |
|
smartymoon Smarty Rookie
Joined: 29 Apr 2003 Posts: 15
|
Posted: Fri Jun 17, 2005 3:51 pm Post subject: |
|
|
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 |
|
timmerk Smarty Regular
Joined: 06 Mar 2005 Posts: 36
|
Posted: Sat Jun 18, 2005 9:17 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Jun 18, 2005 9:23 pm Post subject: |
|
|
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 |
|
timmerk Smarty Regular
Joined: 06 Mar 2005 Posts: 36
|
Posted: Sat Jun 18, 2005 9:25 pm Post subject: |
|
|
I meant have section work with data returned from simplexml. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Jun 18, 2005 9:32 pm Post subject: |
|
|
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 |
|
timmerk Smarty Regular
Joined: 06 Mar 2005 Posts: 36
|
Posted: Sat Jun 18, 2005 9:33 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Jun 18, 2005 9:54 pm Post subject: |
|
|
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 |
|
timmerk Smarty Regular
Joined: 06 Mar 2005 Posts: 36
|
Posted: Sat Jun 18, 2005 9:56 pm Post subject: |
|
|
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 |
|
|