CodeBehind View Handlers


Basic View Handlers


View Handlers are methods within Class CodeBehind pages that allow you to control where and when your content is displayed. In the CodeBehind documentation so far, you've seen examples of a view handler named "function area2()".

This function is a view handler that is automatically called to render content in "area2" of a page (area2 is usually the center of a page). The TextSide engine will automatically look for any functions named area1() --> area5() and render them if they are available. These functions are expected to return a string of html that represents their content.

Where areas are actually rendered depends on the layout that is currently being used by your page. A layout file, located in your site\themes\theme_name\layouts\ directory determines how different areas are displayed on a page. For more information on layouts, please see the theme documentation.

Usually as a rule of thumb:

area1 = Left hand column
area2 = Center page
area3 = Right hand column
area4 = Top of page
area5 = Bottom of page

For Example:

<?php
class pageHello3 extends page {
    var 
$layout "Columns2";
    
/*===========================================================
    AREA 1 - Left Column
    ============================================================*/
    
function area1(){
        return 
"This the left hand column of the page!";
    }

    
/*===========================================================
    AREA 2 - Center Page
    ============================================================*/
    
function area2(){
        return 
"This is the center of the page!";
    }
}
?>
This would be rendered as:
viewhandlers.jpg

View Handler Properties


View handlers have two properties that can be set within them that affect how they are rendered:

$this->border - Can be set an int that corresponds to a border defined in the current theme. This will cause the area to be surrounded by a border or background of some time. It defaults to 0 (no border).

As a general rule 0 = no border and 1 = a simple text title above area contents.
Any numbers above 1 are dependent on the theme currently used. Please see the Theme documentation on borders and how to create customized borders.

$this->title - Sets the title of the area. This is sometimes used by borders.

Heres the same example as before, but using the these properties:

<?php
class pageHello3 extends page {
    var 
$layout "Columns2";
    
/*===========================================================
    AREA 1 - Left Column
    ============================================================*/
    
function area1(){
        
$this->title "Navigation";
        
$this->border 2;
        return 
"Navigation goes here.";
    }

    
/*===========================================================
    AREA 2 - Center Page
    ============================================================*/
    
function area2(){
        
$this->title "Hello World!";
        
$this->border 1;
        return 
"Normal page contents go here.";
    }
}
?>
This would be rendered as follows: (this will differ depending on the theme you are currently using)
viewhandlers2.jpg

Advanced - Multiple View Handlers in the same Area


CodeBehind pages can also place multiple view handlers in the same area. For example, lets say in the above page you wanted another set of content immediately below the navigation - perhaps the the latest news or site status. You can do this by creating another view handler named:
area1_1(). You can create other view handlers by appending increasing numbers behind them, for example: area1_2(),area1_3(), etc.
For example:

<?php
class pageHello3 extends page {
    var 
$layout "Columns2";
    
/*===========================================================
    AREA 1 - Left Column
    ============================================================*/
    
function area1(){
        
$this->title "Navigation";
        
$this->border 2;
        return 
"Navigation goes here.";
    }
    function 
area1_1(){
        
$this->title "Site Status";
        
$this->border 2;
        return 
"The site is live!";
    }

    
/*===========================================================
    AREA 2 - Center Page
    ============================================================*/
    
function area2(){
        
$this->title "Hello World!";
        
$this->border 1;
        return 
"Normal page contents go here.";
    }
}
?>
This would be rendered as:
viewhandlers3.jpg

Advanced - Context Based View Handlers


It is also possible to have your CodeBehind page use different view handlers based on the value of a query string parameter called $view. For example, if your page were called with: hello.php?view=news, you could have it display a different set view handlers. To do this, simply append the name of the desired view parameter needed to view a handler at the end of its method name: area2News(), area1News(), area1News_1(), area1News_2(), etc. Note that handlers appended this way will only be visible when the given query string is present.

Any view handlers without a view name after them are always displayed.

You can also force a CodeBehind page to use a view by default by setting the $this->defaultView member.

Example:

<?php
class pageHello3 extends page {
    var 
$layout "Columns2";
    var 
$defaultView "Home"//Causes any view handlers for Home 
                   //to be displayed if no $view query
                   //variable is set.
    /*===========================================================
    AREA 1 - Left Column
    ============================================================*/
    //This view handler doesn't have a name after it - it will 
    //always be visiable
    
function area1(){      
        
$this->title "Navigation";
        
$this->border 2;
        return 
"Navigation goes here.";
    }

    
/*===========================================================
    AREA 2 - Center Page
    ============================================================*/
    //This view handler will only be visible if ?view=News is set
    
function area2News(){
        
$this->title "News";
        
$this->border 1;
        return 
"News";
    }
    
//This view handler will be visible if ?view=News is set or if no
    //view is set at all (since "Home" is the default view)
    
function area2Home(){
        
$this->title "Hello World!";
        
$this->border 1;
        return 
"Normal page contents go here.";
    }
}
?>
This would be rendered different depending on the url enter:

http://www.site.com/hello3 or
http://www.site.com/hello3?view=home
view1.jpg

http://www.site.com/hello3?view=news
view2.jpg