Parts will 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.
Using templates in Parts is
exactly the same as using templates in CodeBehind pages. For a basic overview of how templates work, you should check out the
CodeBehind Templates Documentaiton
To use templates for parts, first place your template file in your part's bin\templates directory. Next, call
$this->fetch("templatename.tmpl.php") from your Part's view handler.
Here is an sample, continuing the Current Time Part:
Directory Structure:
--------------------
partTime\
|--info.php
|--partTime.php
|--bin\
|--templates\
|--time.tmpl.php
partTime.php:
---------------
<?php
class partTime extends part {
//the view that should be shown by default
var $defaultView = "Content";
/* View Handler - Content */
function viewContent(){
//get the current time
$time = date("F j, Y, g:i a");
//pass it to the template
$this->set("time",$time);
//call the template
return $this->fetch("time.tmpl.php");
}
} ?>
time.tmpl.php
-------------------
The current time is <b><?=$time?></b>!
This part would appear as:
All templates for your part are automatically passed some special variables. For a description of these variables, please see the
CodeBehind Page Templates documentation (near the bottom of the page).
In addition to the variables used for CodeBehind Pages, part templates are passed the following extra variables:
$id and $iid - This variable contains the id of your specific part. If you wish to direct events or view handlers for your part, you will need to use this $id in your link. This allows you to direct events to your part in particular instead of interfering with other parts on the page.
$defaultView - The default view handler for your part. (See
View Handlers for more information)