Section 4: XPHP Types

Built-in XPHP Types

So far we have seen two XPHP types: 1) a PHP variable 2) a PHP function. However, there are a few other kinds of XPHP tags that you can use. The built-in types are:

Each of these types is capable of using the forms of output manipulation covered later in this guide.

After a brief overview of the built-in XPHP types, you will see how you can extend XPHP to include your own user-defined types as well.

XPHP Types: File

The file type simply returns the contents of a file located at a specified path:

<xphp file="/path/to/file"/>

<xphp file="/path/to/file">
    <mod id="foo"/>
</xphp>
	

XPHP Types: Content

The content type allows inline XHTML to be displayed through XPHP. This is useful when combined with if statements (which will be covered later in this guide), so that content can be modified or conditionally included in your page.

XHTML content should be supplied within a <content> tag.

<xphp content="true">
    <content>
        <p class="foo">Some valid XHTML content.</p>
    </content>
</xphp>
	

XPHP Types: XSLT

The XSLT type allows inline XML to be transformed with a specified stylesheet, returning the resulting output. To do this, use the xslt attribute.

There are two ways to perform an XSLT transformation. You can supply paths to the stylesheet and xml data, or you can include them inline.

<!-- Using paths: -->
<xphp xslt="true" stylesheet="/path/to/stylesheet" xml="/path/to/xml"/>

<!-- Using inline: -->
<xphp xslt="true">
	<xml>
	    <booklist>
	        <book>
	            <title>War and Peace</title>
	            <author>Leo Tolstoy</author>
	        </book>
	        <book>
	            <title>The Hearing Trumpet</title>
	            <author>Leonora Carrington</author>
	        </book>
	    </booklist>
	</xml>
	<stylesheet>
	    ... stylesheet ...
	</stylesheet>
</xphp>
	

You can also combine the two, where your stylesheet is included via path and the xml inline, or vice versa.

User-Defined XPHP Types

It's very easy to add your own types to XPHP, and the possibilities this affords developers are endless. To register a new type, you must call registerType() from the XPHP class after initializing it:

$xphp=new XPHP();
$xphp->registerType('foo','myFooHandler');
	

In the above example, we have registered an XPHP type "foo" which is handled by the function myFooHandler().

<?php
$xphp=new XPHP();
$xphp->registerType('foo','myFooHandler');

function myFooHandler($xphp) {
$fruit=array('apple','orange','passionfruit');
return $fruit[(int)$xphp['foo']];
}

?>

<!-- prints "orange" -->
<xphp foo="1"/>
	

Your new type can be structured in any way you want. Consider the following "language" type.

<xphp language="PHP">
	<version>5</version>
	<link type="homepage">http://www.php.net</link>
	<description>A widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.</description>
</xphp>
	

Note: For the sake of consistency, user-defined types cannot override the built-in XPHP types. Any handlers you register will only fire if not first intercepted by the built-in types, and they will be matched in the order that they were defined.

Lastly, user-defined types can also be registered for an object. This is done by passing in the object as an optional third parameter of registerType():

$xphp=new XPHP();
$handlerObject=new XPHPHandlers();
$xphp->registerType('foo','myFooHandler',$handlerObject);
	

This means that all XPHP tags of type "foo" will be passed to $handlers->myFooHandler().

User-Defined XPHP Tags

Defining XPHP types is useful, but you can really take full control of the templating engine by registering user-defined tags. By registering a new tag, you are telling XPHP to parse those tags the same way it would an <xphp> tag.

Adding this to the above example gives us the following ability:

<?php
$xphp=new XPHP();
$xphp->registerTag('language');
$xphp->registerType('name','nameHandler');

function nameHandler($xphp) {
return 'This language is '.$xphp['name'].', version '.$xphp->version.'.';
}
?>

<!-- prints "This language is PHP, version 5.": -->
<language name="PHP">
	<version>5</version>
	<link type="homepage">http://www.php.net</link>
	<description>A widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.</description>
</language>
		

Security: The XPHP engine parses all tags in a single pass. This means that if an <xphp> tag returns dynamic content containing a subsequently registered tag, the custom tag in the dynamic content will not be parsed.

« Previous Section | Section 5 of 6: Advanced Output Manipulation »