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

using smarty with xml

 
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
omame
Smarty n00b


Joined: 01 Nov 2003
Posts: 2

PostPosted: Sat Nov 01, 2003 6:01 am    Post subject: using smarty with xml Reply with quote

I have inherited a small project, where source material is in a single XML file (it's for a small website). At first, I was thinking of using patXMLRenderer or phptaglib, but am investigating whether I can transform this XML content using smarty as well. Has anyone general advice/examples/tutorials to point me towards?

thanks
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sat Nov 01, 2003 7:25 am    Post subject: Reply with quote

I like your thinking and while it is true that you can use Smarty to create templates that generate XML, you mentioned transformation. If you mean something along the lines of what XSLT does but using Smarty, then I have the feeling you won't be satisfied. Particularly, Smarty does not transform nodes nor does it even have a concept of a node. Further, Smarty is not intended to do data processing as it introduces unneccessary programmatic concepts into the templates.

On the other hand, if you are bent on rolling your own, very much luck to you! Also, check out this popular forum post on recursive template functions and macros. Further, you may find a novel way to get Smarty to behave this way. If so, please update us!
Back to top
View user's profile Send private message
omame
Smarty n00b


Joined: 01 Nov 2003
Posts: 2

PostPosted: Sat Nov 01, 2003 2:40 pm    Post subject: xml Reply with quote

By transformations, I basically mean taking the content in the XML file and
outputting it to the client device protocol (eg html, xhtml, wap, etc). Thanks for the advice, though. I'll have to think about whether it's better convert the source file to something compatible with Smarty, or whether I'll be forced to roll something on my own.

If there is anything, no matter how primitive, showing how to interface an XML file and Smarty, do let me know!
Back to top
View user's profile Send private message
dabase
Smarty n00b


Joined: 26 Jul 2003
Posts: 4

PostPosted: Fri Dec 05, 2003 2:39 am    Post subject: something very dynamic Reply with quote

Turning XML to a object or an array data structure is a very good way to build smarty plugin systems that can be configured.

A Example:
XML from QTDesigner with my own tags added

....
<widget class="QLabel" template="textLabelWidget.html">
<property name="name">
<cstring>TextLabel10_2_2</cstring>
</property>
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>70</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Beschreibung</string>
</property>
</widget>......

could be phpized and result in a oo data structure:
with a small function called xml2object or another php/xml method.

The resulting class should have enough properties to be represented in smarty.

If you like to change the admin view you set the widgets vie the xml configirationfile.

!pseudocode! here:

$xml = load("label.xml");
$xmlO = xml2objectMethodOfYourChoice($xml);
$smarty->assign('configuration', $xmlO);
// the widget configuration in the xml Decides how the wisget represents itself
// optionally assign some values for external data (db data etc)
$smarty->assign('value', "a value");
$smarty->assign('configuration', $xmlO->widget['template']);
return $smarty->fetch($xmlO->widget['template']);

Another one:
http://support.codeaddict.de/index.php?module=pnQTKit&type=user&func=form16
This is a simple example of a gui generated by a xml->xslt->php another way of using a smarty plugin system with xml.

For performance reasons the xml .ui file fromQT Designer is xslted into a class constructor for a so called Composition Pattern which represents the widgets behaviour very fine.
// new Widget
$widget = new QLabel();
$args = array();
$args['cstring'] = 'TextLabel9_2';
$widget->set_property('name', new name($args));
unset($args);
$args = array();
$args['x'] = '10';
$args['y'] = '160';
$args['width'] = '80';
$args['height'] = '20';
$widget->set_property('geometry', new geometry($args));
unset($args);
$args = array();
$args['text'] = 'Erweiterter Text';
$widget->set_property('text', new text($args));
unset($args);


The above stuff example leads me to thoughts where i wanna have a xml filtered via xpath to object mechanism in smarty templates itself to provide datastructures and data for my designers. This could be a solution to the old problem what is available where and how is it named and template auhors could completely depend on their own data structures.

I havent figured out all options of smarty and xml , so this may be not popular solutions, but they work and i hope this matches the topic.

My final conclusion is that xml data makes fun if the transformation to application data doesnt require to much effort (e.g handler writing). Maybe XML should be transformed by xslt which is more related to it than smarty. so xml trasformation should be easier with xslt and a php application can do more computations with a object. But !!! Fear your colleagues hating you for starting teaching them xslt, after you introduced smarty 3 weeks ago G* Wink

.. smarty is much more simple and not as general in design here.

Sebastian
http://c0d3.de/html/
Back to top
View user's profile Send private message
tgreenlaw
Smarty Rookie


Joined: 14 Feb 2004
Posts: 6

PostPosted: Sat May 01, 2004 11:13 pm    Post subject: Reply with quote

Once you hit PHP5 + Smarty, XSLT just doesn't cut it any more Very Happy The addition of SimpleXML to PHP makes things SO much easier.

<?php
Header("Content-type: text/xml");
require 'libs/Smarty.class.php';
$smarty = new Smarty;

$template = filename of the new xml template
$xmltext = the xml data sucked out of the database or someplace else.

$xml = simplexml_load_string($xmltext);
$smarty->assign('xml', $xml);
$smarty->display($template);
?>

In your template file, use {$xml->Node->Node->Value} syntax to dereference.

Way too much fun to be getting paid for Very Happy
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon May 03, 2004 5:58 am    Post subject: Reply with quote

tgreenlaw, that's a great tip on how to use a really valuable new feature in PHP5.
Back to top
View user's profile Send private message
totoole
Smarty Rookie


Joined: 10 Nov 2003
Posts: 7

PostPosted: Sun Apr 03, 2005 6:54 pm    Post subject: Smarty simplexml help Reply with quote

I am trying to use {foreach} to iterate through a PHP simplexml object, without success. Can anyone show me a template that does this? Thanks in advance.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Apr 04, 2005 4:39 am    Post subject: Re: Smarty simplexml help Reply with quote

totoole wrote:
I am trying to use {foreach} to iterate through a PHP simplexml object, without success. Can anyone show me a template that does this? Thanks in advance.


Iterating PHP5 objects is not officially supported and if you are using Smarty < 2.6.8 then you won't get any love at all.

Try the latest version of Smarty which no longer explicitly blocks object iteration.
Back to top
View user's profile Send private message
totoole
Smarty Rookie


Joined: 10 Nov 2003
Posts: 7

PostPosted: Thu Apr 07, 2005 7:56 pm    Post subject: sample template? Reply with quote

The foreach syntax is not so easy for me to grasp. What would the template look like that iterated over a simplexml object?

{foreach from=$xmlobject item=item}
<p>$item->node->value</p>
{/foreach}

Am I even close?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Apr 07, 2005 10:03 pm    Post subject: Reply with quote

I haven't tried in a long time. Look at the example on http://php.net/simplexml and assume that you do a:

[php:1:22a6eadedf]include 'example.php';
include 'Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('xml', simplexml_load_string($xmlstr));
$smarty->display('example.tpl');
[/php:1:22a6eadedf]

-- example.tpl --
Code:
{foreach from=$xml item=movie}
  {$movie->plot}<br />
{/foreach}


You'll have to fiddle around until you get used to it because SimpleXML does a lot of magic when it comes to objects/arrays.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri Apr 08, 2005 2:03 am    Post subject: Reply with quote

I was having a little fun. Here's a standalone template you can use to fiddle with simplexml.

[php:1:8d9fe8cc45]include 'Smarty.class.php';
$smarty = new Smarty;
$smarty->display('example.tpl');[/php:1:8d9fe8cc45]
Code:

{* generate some xml *}
{capture assign=xml_data}
<?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 ActÓr</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
{/capture}

{* create a temporary simplexml object and iterate it *}
{foreach from=$xml_data|simplexml_load_string item=movie key=key}
  $key: {$key}, $movie->plot: {$movie->plot}<br />
{/foreach}

{* create a simplexml object as a template variable *}
{assign var=simplexml value=$xml_data|simplexml_load_string}

{* use xpath to select 'rating' nodes and show attribute access *}
{foreach from=$simplexml->xpath('/movies/movie/rating') item=node key=key}
  $key: {$key}, $node: {$node}, $node.type: {$node.type}<br />
{/foreach}


Produces:
Quote:
$key: movie, $movie->plot:
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
<br />


$key: 0, $node: 7, $node.type: thumbs<br />
$key: 1, $node: 5, $node.type: stars<br />


Have Fun!
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
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