Part View Handlers


Overview


Part View Handlers are methods within your part that control how it should be rendered. Part View Handlers are similar to CodeBehind View Handlers.

When it comes time to display your part, TextSide will call one of your View Handlers to render content ( a method named viewName ). The name of the View Handler called is based on the value of the variable vID (where ID is your part ID) in either GET or POST. If vID is not set, a default View Handler will be called.

Note that View Handler for Parts are 'sticky' - the last View Handler rendered is saved in the user's session and will be rendered by default next time, even if it is not specified in the query string.

Heres an example of a simple Time part with two View Handlers. One View Handler shows the current time with a link to the second View Handler that shows the current date.


partTime.php
-----------------
<?php
class partTime extends part {
    
//the view that should be shown by default
    
var $defaultView "Time";
    
    
/* View Handler - Shows Time*/
    
function viewTime(){
        
//get the current time
        
$time date("g:i a");
        
//pass it to the template
        
$this->set("time",$time);
        
//render
        
return $this->fetch("time.tmpl.php");
    }
    
/* View Handler - Shows Date*/
    
function viewDate(){
        
//get date
        
$date date("F j, Y");
        
//pass it to the template
        
$this->set("date",$date);
        
//render
        
return $this->fetch("date.tmpl.php");
    }
?>


time.tmpl.php
-----------------
The current time is <b><?=$time?></b>!
<br />
<a href="<?=$PHP_SELF?>?v<?=$id?>=date">Show Date</a>


date.tmpl.php
-----------------
The current date is <b><?=$date?></b>!
<br />
<a href="<?=$PHP_SELF?>?v<?=$id?>=time">Show Time</a> 

When the user first views the part, it would show the time:

time1.jpg

And when they click on the link, it would show them the date:

time3.jpg

Creating Links to Views Handlers


In order to switch between View Handlers of your Part, you must put "vID=desiredView" into your query string (where ID is the id of your part).

This is used within the time and date templates above:


<a href="<?=$PHP_SELF?>?v<?=$id?>=date">Show Date</a>

Here, the template provide a link to the current page (<?$PHP_SELF?>) and sets the View Handler for its part (v<?=$id?>) to "date". The template variable $id is a special variable that contains the id of the current part. It is automatically passed to any templates. See Part Templates and CodeBehind Templates for more details on special template variables.

Differences between Part and CodeBehind View Handlers


The main differences between Part and CodeBehind View Handlers are:
  • Part View Handlers can not control where on the page they are rendered (no area1, area2, etc.). This is because a user will be dragging and dropping a part to one of these areas on their own.

  • Part View Handlers can not be stacked to render multiple sections within the same area. (no area1_1, area1_2, etc.)

  • Part View Handlers are selected based on the value of the $vID variable in the query string (where ID is the id of the part, which can be access by $this->id within the part, or $id from within a template.)