eXtension Templates for PHP (XTP for short)

WebControlX class - using properties

In order to facilitate communication between master template and its components you can use component properties. Here is simple example of one page component (its template and its code file) and invoking component from page:

    (greeting.phpx - template file)
    
<@
    <option name="class" value="Greeting"/>
    <option name="source" value="greeting.php"/>
@>
Hello <strong>${$this->Name}!</strong>

Here we can see that this component uses its property Name when displaying greeting text.

    (greeting.php - code file)
    
<?php
require_once(dirname(__FILE__).'/xtp/xtp.php');

class Greeting extends WebControlX {
    public $Name;
}

?>

Code file only declares class Greeting (which is mentioned in template) and declares its property Name.

    (index.phpx - template file)
    
<@
    <option name="class" value="Index"/>
    <option name="source" value="index.php"/>
    <control tag="hello" prefix="x" path="greeting.phpx"/>
@>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>Hello world</title>
    </head>
    <body>
        <x:hello Name="Ivan"/>
    </body>
</html>

This main page template file finally uses greeting control to greet user, but this time dynamically passing user's name to greeting control (as value for its property 'Name' - <x:hello Name="Ivan"/>).

    (index.php - code file)
    
<?php

require_once(dirname(__FILE__) . '/xtp/xtp.php');

class Index extends WebControlX {
}

$page = WebControlX::LoadControl('index.phpx');
if ($page)
	print $page->Render();
else
	print 'Error loading page template!!!';

?>

Main code file only declares Index class for main page and puts things in motion by loading page template and later rendering its content to the client browser.

ID - Special Property

All web controls implement special property "ID" (case insensitive) allowing you to declare member property for your class from within template file (.phpx file). You use this property in .phpx file like in this example:

<php:RepeaterX id="Rep" DataSource='${array("John", "Thomas", "Marry", "Ivan")}'>
    <HeaderTemplate>
        Hello <span>
    </HeaderTemplate>
    <ItemTemplate>${$this->Item}</ItemTemplate>
    <SeparatorTemplate>${$this->Index != 3 ? ', ' : ' and '}</SeparatorTemplate>
    <FooterTemplate>
        </span>
    </FooterTemplate>
</php:RepeaterX>

Later you can access this control from your .php file the same way as it was declared as property:

function PreRender() {
    if (self::$Session['user'] == null)
        $this->Rep->Visible = false;
}

« Previous
Starting XTP engine
Next »
WebControlX - base class