Share » Forums » Install & configuration » EZ XML

EZ XML

EZ XML

Wednesday 22 January 2003 1:10:19 pm - 6 replies

Author Message

Jan Borsodi

Monday 27 January 2003 6:30:31 am

> Does anyone know how to use eZ XML to load information from
> an XML file into an eZ Publish template? The SDK examples
> show the use of eZ XML in a php file, but I don't think php
> will work in a .tpl file. I tried to use the SDK example in
> template, but it doesn't seem to work. Here's what I put in
> a class edit template, both with and without the {literal}
> tags.
>
> {literal}
> <?php
> include_once( "/lib/ezxml/classes/ezxml.php" );
>
> $xml = new eZXML();
> $testDoc = "<?xml version='1.0'?>
> <doc>
> <article>
> <paragraph name='Introduction'>Paragraph
> contents</paragraph>
> <paragraph name='Story'>Story paragraph
> contents</paragraph>
> </article>
> </doc>";
>
> $dom =& $xml->domTree( $testDoc );
>
> print( "<b>XML document to parse:</b><br>" );
>
> print( nl2br( htmlspecialchars( $testDoc ) ) );
>
> print( "<br><b>Results from parsing:</b><br>" );
> $paragraphs =& $dom->elementsByName( "paragraph" );
>
> foreach ( $paragraphs as $paragraph )
> {
> // get the name of the item, should be paragraph
> print( "<br/>New " . $paragraph->name() . ":<br/>" );
>
> // print the value of the name attribute
> print( "Name: " . $paragraph->attributeValue( "name" ) .
> "<br/>" );
> print( "Content: " . $paragraph->textContent() . "<br/>"
> );
> }
>
>
> ?>
> {/literal}

We won't allow PHP code in the templates by default, however you could write a template function which executes whatever is present as it's children.

Another approach to this is to have an xmltree operator which generates the xml dom tree and returns it. That way it's possible to go trough the tree to get the data you need.

A third option is to have an XSLT operator which would transform the xml into html according to an xslt file.

The two last solutions might be included in the standard release in the future, but we cannot guarantee when it will happen (if it will happen).

--
Amos

Documentation: http://ez.no/ez_publish/documentation
FAQ: http://ez.no/ez_publish/documentation/faq

Jan Borsodi

Monday 27 January 2003 6:35:03 am

> Does anyone know how to use eZ XML to load information from
> an XML file into an eZ Publish template? The SDK examples
> show the use of eZ XML in a php file, but I don't think php
> will work in a .tpl file. I tried to use the SDK example in
> template, but it doesn't seem to work. Here's what I put in
> a class edit template, both with and without the {literal}
> tags.
>
> {literal}
> <?php
> include_once( "/lib/ezxml/classes/ezxml.php" );
>
> $xml = new eZXML();
> $testDoc = "<?xml version='1.0'?>
> <doc>
> <article>
> <paragraph name='Introduction'>Paragraph
> contents</paragraph>
> <paragraph name='Story'>Story paragraph
> contents</paragraph>
> </article>
> </doc>";
>
> $dom =& $xml->domTree( $testDoc );
>
> print( "<b>XML document to parse:</b><br>" );
>
> print( nl2br( htmlspecialchars( $testDoc ) ) );
>
> print( "<br><b>Results from parsing:</b><br>" );
> $paragraphs =& $dom->elementsByName( "paragraph" );
>
> foreach ( $paragraphs as $paragraph )
> {
> // get the name of the item, should be paragraph
> print( "<br/>New " . $paragraph->name() . ":<br/>" );
>
> // print the value of the name attribute
> print( "Name: " . $paragraph->attributeValue( "name" ) .
> "<br/>" );
> print( "Content: " . $paragraph->textContent() . "<br/>"
> );
> }
>
>
> ?>
> {/literal}

We won't allow PHP code in the templates by default, however you could write a template function which executes whatever is present as it's children.

Another approach to this is to have an xmltree operator which generates the xml dom tree and returns it. That way it's possible to go trough the tree to get the data you need.

A third option is to have an XSLT operator which would transform the xml into html according to an xslt file.

The two last solutions might be included in the standard release in the future, but we cannot guarantee when it will happen (if it will happen).

--
Amos

Documentation: http://ez.no/ez_publish/documentation
FAQ: http://ez.no/ez_publish/documentation/faq

Paul Borgermans

Monday 27 January 2003 7:40:19 am

> Another approach to this is to have an xmltree operator
> which generates the xml dom tree and returns it. That way
> it's possible to go trough the tree to get the data you
> need.

Seems the most logical way now, useful for small xml objects

>
> A third option is to have an XSLT operator which would
> transform the xml into html according to an xslt file.
>
> The two last solutions might be included in the standard
> release in the future, but we cannot guarantee when it will
> happen (if it will happen).

We need it too, the XSLT operator I mean. And we will write a module for it if necessary, probably in the next 6 months after 3.0 final. It is desirable for incorporating external XML content: through SOAP enabled services for example, or even better to get Docbook XML support. There are basically two options: do everything with php libraries or let a backend do the transformations (apache cocoon for example, it would also handle the caching of results). Any comments?

Paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Paul Borgermans

Monday 27 January 2003 7:42:05 am

> > We won't allow PHP code in the templates by default,
> however
> > you could write a template function which executes
> whatever
> > is present as it's children.
>
> I have a script like this:
> <?
> ob_start();
> /*
> script with calculations and html output here
> */
> $buf = ob_get_contents();
> ob_end_clean();
> ?>
>
> Translating some scripts into EzP language will take ages.
>
> Is there no way to use $buf in the template parser?

That appears to be trivial, why not set $buf as a single variable in a template which surrounds this by <div ....> </div> ?

Paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Paul Borgermans

Monday 27 January 2003 10:03:21 am

> > That appears to be trivial, why not set $buf as a single
> > variable in a template which surrounds this by <div ....>
>
> > </div> ?
>
> I think I've overlooked something. How can I execute a
> script from inside a template? Can you give me an example?

No, it is the other way around: you set the values of a variable in the template through a php script.

To call php code (in a module) from a template, the "fetch" template function should be used as far as I understand. In any case this php code should then fill a template.

We miss the docs on this for more details :-(

Paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Jan Borsodi

Monday 27 January 2003 12:29:58 pm

> > That appears to be trivial, why not set $buf as a single
> > variable in a template which surrounds this by <div ....>
>
> > </div> ?
>
> I think I've overlooked something. How can I execute a
> script from inside a template? Can you give me an example?

The usual way is to set template variables from the PHP before the template is executed.
$tpl->setVariable( "buf", $buf );

or you can create custom template functions and/or operators to do the work.

The fetch system can be used to execute functions in a class, see kernel/content/function_definitions.php for some examples.

Also the PHP operator defined in lib/eztemplate/classes/eztemplatephpoperator.php can be used to map php functions to template operators easily, create your own custom functions in PHP and map them with this class.

--
Amos

Documentation: http://ez.no/ez_publish/documentation
FAQ: http://ez.no/ez_publish/documentation/faq

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu