Section 3: Complex Usages

Passing Arguments to Function Calls

Calling a function with an XPHP tag really becomes useful once named arguments can be passed to that function. A function called by XPHP has two args.

First, the <xphp> tag is passed to the function in the form of a SimpleXML object.

Second, the function's arguments are passed in. XPHP does this via the <arg> tag. All supplied args are passed to the function in an array, in the order that they were specified.

In the following example, the function foo() will output the value of arg "bar".

<?php
require '/path/to/class.xphp.php';
$xphp=new XPHP();

function foo($xphp,$args) {
return $xphp['name'].' says '.$args['bar'];
}
?>

<!-- prints "test says hello world": -->
<xphp name="test" function="foo">
    <arg id="bar">hello world</arg>
</xphp>
	

However ...

One of the wonderful things about XPHP is that the arguments are passed as SimpleXML objects. This means that they can be used as regular strings (as in the above example) but are also capable of much more:

<?php
require '/path/to/class.xphp.php';
$php=new XPHP();

function foo($xphp,$args) {
return '<span class="'.$args['bar']['class'].'">'.$args['bar'].'</span>';
}
?>

<!-- prints "<span class="baz">hello world</span>": -->
<xphp function="foo">
    <arg id="bar" class="baz">hello world</arg>
</xphp>
	

... and also ...

<?php
require '/path/to/class.xphp.php';
$php=new XPHP();

function booklist($xphp,$args) {
return $args['book']->title;
}
?>

<!-- prints "War and Peace": -->
<xphp function="booklist">
    <arg id="book">
        <title>War and Peace</title>
    </arg>
</xphp>
	

A Complete Example

Lastly, if multiple "foo" arguments are passed in, then the function will be passed an array of "foo" args: foreach($args['foo'] as $foo) ...

If we combine all these things into a working example, it might look something like this:

<?php
require '/path/to/class.xphp.php';
$php=new XPHP();

function booklist($xphp,$args) {
$output='';
if (isset($args['book'])) {
    foreach($args['book'] as $book) $output.='<div class="'.$book['class'].'">'.$book->title.' by '.$book->author."</div>\n";
}
else $output='<p>There are no books in your book list!</p>';
return $output;
}
?>

<xphp function="booklist">
    <arg id="book" class="unread">
        <title>War and Peace</title>
        <author>Leo Tolstoy</author>
    </arg>
    <arg id="book" class="read">
        <title>The Hearing Trumpet</title>
        <author>Leonora Carrington</author>
    </arg>
</xphp>
	

This would print:

<div class="unread">War and Peace by Leo Tolstoy</div>
<div class="read">The Hearing Trumpet by Leonora Carrington</div>
	

Passing Arguments to Object Methods

Often times the function you want to call with XPHP will be a public function of an object. In order to tell XPHP to refer to an object when requesting a function's output, simply use the object attribute to target an object in the global scope:

<?php
require '/path/to/class.xphp.php';
$xphp=new XPHP();

class Foo {

public function bar() {
return 'hello world';
}

}

$foo=new Foo();
?>

<xphp function="bar" object="foo"/> <!-- prints "hello world" -->
	

« Previous Section | Section 4 of 6: XPHP Types »