CodeBehind Templates


CodeBehind pages often make use of the built in template system.

Templates are a method of separating your page's logic from its design. It is considered good design practice to use templates in this way, as it makes it easier to maintain your site and for others to read your code.

A Template file is simple file that contains only HTML and PHP variables that are echoed or looped. The contents of these variables is determined elsewhere and passed to the template to render. For example, consider the following simple file and template that determines the name of the user in the database with ID 1:


CodeBehind Page:
---------------
<?php 
//perform logic (connecting to database, calculations, etc.)
$result $sql->QueryItem("SELECT username FROM users WHERE id='1'");

//create a template
$template = new template();

//pass variables to the template. the template only has
//access to variables that we pass here.
$template->set("username",$result);

//render the template
$html $template->fetch("MyTemplate.tmpl.php");
?>

MyTemplate.tmpl.php
-------------------
<h1>Sample Test!</h1>
The first user in the database is <?=$username?>!

Templates with Raw PHP CodeBehind Pages


When using a raw PHP CodeBehind page, the TextSide engine automatically sets up a single template instance named $template and has it pointed to your files bin\template directory. You can use this templates fetch($filename) method to call a specific template. For example:


Directory Structure:
--------------------
control\
|--hello.php
|--bin\
   |--templates\
      |--hello.tmpl.php

Hello.php
--------------------
<?php
global $siteUser;
$name $siteUser->username;
$template->set("name",$name);
echo 
$template->fetch("hello.tmpl.php");
?>

Templates with Class CodeBehind Pages


When using a CodeBehind page that implements a page class, the page class automatically inherits access to a template instance. As with a the raw php page, this template is already pointed to the pages bin\templates directory. You can access this template by using either $this->template or by directly calling $this->set() and $this->fetch() For example:


<?php
class pageHello extends page {
    var 
$layout "Columns1";
    
/*===========================================================
    AREA 2 - Center Page Content
    ============================================================*/
    
function area2(){
        
$this->title "Hello World!";
        global 
$siteUser;
        
$name $siteUser->username;
        
$this->set("name",$name);
        return 
$this->fetch("hello.tmpl.php");
    }
}
?>
This would display the same content as the raw php CodeBehind page, but it would fit in with the sites theme.

Special Template Variables


CodeBehind templates are automatically passed some special variables to make coding easier. The special variables passed are:

$directory - Provides an absolute path to your CodeBehind pages bin directory. This can be used to easily link files and photos. For example: <img src="<?=$directory?>images/logo.gif">

$baseDirectory - Provides a relative path to your CodeBehind page from the site/control directory. For example, if you page were in site/control/tests/myTest.php, this would contain "tests/".

$PHP_SELF - The same as $_SERVER["PHP_SELF"] - this provides an absolute path to your current page. This is useful for form submittals and links that you want to return to the same page.

$PHP_SELFDIR - An absolute path to the current pages directory. For example, if your page were in http://www.yoursite.com/tests/myTest.php, this would contain "http://www.yoursite.com/tests/"

$siteRoot - Contains the absolute root url of your website. This is useful for creating absolute urls. For example, for this site it would be: "http://www.textside.com/"

$siteUser - A reference to the global $siteUser that contains information about the current user. Sett globals for more information.

$eventResult - (Class CodeBehind Pages Only). Contains the result of an Event Handler in boolean (true/false) form. See the Event Handlers section for more details.

$eventMessage - (Class CodeBehind Pages Only). Contains any message returned as a result of an Event Handler. See the Event Handlers section for more details.

$eventResultString - (Class CodeBehind Pages Only). Contains the $eventResult boolean in the string form of "1" or "0". This is useful for applying CSS classes to a string of result text. See the Event Handlers section for more details.

Notes


Note that the above examples show simple cases for templates. In these cases a template is a little over kill. However, in most pages that you work with, templates can greatly simplify your work and make your site easier to manage and code.