eXtension Templates for PHP (XTP for short)
Logic behind templates - PHP
As we already saw every but the most trivial components need to have class behind it in order to be able to better control its behavior. We will again illustrate how this all works with simple example -- navigation.php file corresponding to before mentioned navigation.phpx file:
<?php
class Navigation extends WebControlX {
public $Menu;
public function PreRender() {
if (self::$Session['user']) {
$this->Menu = '<ul>
<li><a href="/phpx/index.php">Your Tickets</a></li>
<li><a href="/phpx/index.php?change_data=-7">Account Data</a></li>
<li><a href="/phpx/index.php?logout=c6h8">Logout</a></li>
</ul>';
}
elseif (self::$Session['admin']) {
$this->Menu = '<ul>
<li><a href="/phpx/index.php?logout=c6h8">Logout</a></li>
</ul>';
}
}
}
?>
Looking this simple example we can notice few important things:
- every class representing page component or page needs to be derived from WebControlX class;
- all properties used in template file (in ${} blocks) need to be declared public in class;
- content of properties is set in PreRender function -- this function is inherited from WebControlX class and should be overridden in classes that represent components (this method is invoked prior to generating HTML making it perfect place to play with content);
- This file again mixes HTML and PHP code, and that is bad practice and shouldn't be done.
| « Previous Sample component - HTML | Next » Starting XTP engine |
