eXtension Templates for PHP (XTP for short)
WebControlX - base class for XTP controls
As we have already seen WebControlX is base class for all web components (both pages and smaller controls). All classes derived from WebControlX inherit certain properties they can use to further tune its own behavior.
- Parent (WebControlX) - you can use this property to access control's parent
- Visible (bool) - controls control's visibility
- ClientID (string) - unique ID of control in
page context, this can be convenient for generating
unique ids in HTML - for example template like
<div id="${$this->ClientID}">Hello <strong>${$this->Name}!</strong></div> <script type="text/javascript"> function hide_${$this->ClientID}() { var div = document.getElementById("${$this->ClientID}"); div.style.display = "none"; } </script>Will render HTML very similar to<div id="_wcx_2__phpx">Hello <strong>Milka!</strong></div> <script type="text/javascript"> function hide__wcx_2__phpx() { var div = document.getElementById("_wcx_2__phpx"); div.style.display = "none"; } </script>
There are also few static properties which you can use to access form, session and cookie data. More information on them you can find here.
PreRender Method
Single the most important method in WebControlX class is method PreRender - it is invoked prior to rendering control's content to HTML. You should override this method if you need more complex behavior. By the time this method is called all inner controls are already constructed. PreRender method is invoked in preorder time - PreRender is called on the container control first, and on the contained control after.
The most usual use for PreRender is to set some component's properties in it. For example:
public function PreRender() {
$this->Message = HttpUtilityX::HtmlEncode('Click on ticket to get more
information. Yellowish tickets are not replied yet, greenish tickets are
replied by support stuff and white tickets are closed.');
$this->_List = Ticket::GetTicketsForUser(self::$Session['user']);
}
You can also use PreRender method to do some kind of quick processing before sending user to some other page for further processing, like in this login page example:
class LoginPage extends WebControl {
public $Email;
public function PreRender() {
$this->Email = self::$Request["email"];
if (self::$Request->Form['email'])
self::Login();
}
private function Login() {
if (self::$Request->Form['EMAIL'] == 'admin@softwarehood.com' and
self::$Request->Form['password'] == 'secret') {
self::$Session['user'] = 1;
self::$Response->Redirect('index.php');
}
else
self::$Response->Redirect('index.php?email=' . self::$Request->Form['email']);
}
}
LoadControl Method
WebControlX::LoadControl static method is used to dynamically load template and to create wrapping WebControlX class derived object. Its main use is for starting template engine, but it can also be used if you want to dynamically load different controls in your page. For example, main page template could look similar to this:
<@
<option name="class" value="Index"/>
<option name="source" value="index.php"/>
@>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Support Center</title>
</head>
<body>
${$this->Content}
</body>
</html>
And PreRender method like this
public function PreRender() {
// if user is logged in then show him/her appropriate page,
// otherwise ask for credentials
if (self::$Session['user'])
$this->Content = WebControlX::LoadControl('Controls/user_page.phpx');
else
$this->Content = WebControlX::LoadControl('Controls/login_page.phpx');
// Button logout depressed - clear session data and transfer her/him home
if (self::$Request['logout']) {
self::$Session->Remove('user');
self::$Response->Redirect('index.php');
}
}
This way page contents will be dynamically loaded from either user_page.phpx or login_page.phpx depending whether user is logged in or not.
| « Previous Using control properties | Next » Reading forms, redirecting user |
