Article

eZ publish: PHP's Killer App - Parts 1-3

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Next

The User Interface

Now let's look at one of the files PHP includes, to see what it does (I won't cover all of them, but, again, you'll find them completed in the ZIP at the end -- it's an eZ life!). Let's take include( "ezfaq/user/faqview.php" ); as the example. Here's what it looks like:

<?php                    
// include the class files.                    
include_once( "classes/INIFile.php" );                    
include_once( "classes/eztemplate.php" );                    
include_once( "classes/eztexttool.php" );                    
include_once( "ezfaq/classes/ezfaq.php" );                    
include_once( "ezfaq/classes/ezfaqheading.php" );                    
include_once( "ezfaq/classes/ezfaqcategory.php" );

First, include all the class files we need. Have a look at the documentation for the INIFile, ezTemplate and ezTextTool, so you know what they're doing.

// Instantiate eZFaq classes                    
$category = new eZFaqCategory ($categoryID);                    
$heading = new eZFaqHeading ($headingID);                    
$faq= new eZFaq ($faqID);

This script allows the viewing of a FAQ, so our data supplier script provides us with these variables. We use them to instantiate our logic classes, which in turn prepare the data we want to display. One thing to note as we look back at our data supplier: I'm "re-mapping" values from the $url_array to variables with other names, like $categoryID. This serves two purposes: first it makes the faqview.php script easier to read, and second, if our URL structure changes, we only need to modify the datasupplier.php script.

Next we have:

// Instantiate the INIFile class and set language                    
$ini =& INIFile::globalINI();                    
$Language = $ini->read_var( "eZFaqMain", "Language" );                    
                   
// Instantiate the template class and define the intl file using site.ini                    
$tpl = new eZTemplate( "ezfaq/user/" . $ini->read_var( "eZFaqMain",                      
"TemplateDir" ),                    
                      "ezfaq/user/" . "intl", $Language,                      
"faqview.php" );

This fires up the INIFile class to assign the language variable we placed in site.ini, then starts the eZTemplate class, getting the ]TemplateDir value from site.ini as well, while reading a language file we store as [eZ publish_root]/ezfaq/user/intl/en_GB/faqview.php.ini (we'll see that in a moment).

// Loads all intl template variables for replacement                    
$tpl->setAllStrings();                    
                   
// Defines the template to be used                    
$tpl->set_file( "faqview_tpl", "faqview.tpl" );

setAllStrings() automatically loads international template variables for replacement. Then set_file() tells eZ publish which template file we're using here.

// Set template variable to display current category                    
$tpl->set_var( "category", $category->category() );                    
$tpl->set_var( "category_id", $categoryID );                    
                   
// Set template variable to display current heading                    
$tpl->set_var( "heading", $heading->heading() );

Here we set some template variables for replacement with their PHP equivalent. Notice we're already using methods from our logic classes, namely $category->category(), which returns the current category name and $heading->heading(), which returns the current heading name (the "current" category/heading was defined when we instantiated the classes in this script, you remember, the values coming from the data supplier).

// Define template sections for "looping", such as table rows                    
$tpl->set_block( "faqview_tpl", "category_list_tpl",                      
"category_list" );

"Blocks" in templates are sections we wish to "loop", such as rows in a table or <option />'s in a <select /> tag. The eZ publish template parser needs to know what they're called so it can find them in a template and loop through them.

// Prepare the category_list template for parsing                    
$tpl->set_var( "category_list", "" );

When you use blocks, it's a good idea to start by setting them to an empty string, so that if the array we use to loop through the block has no elements, you won't get template variables turning up in the XHTML.

// Fetch all faq categories                    
$categoryArray = & $category->getAll();

Now we use our getAll() method to fetch a list of categories from the database (in fact an array of objects for each row in the ezfaq_category table). This allows use to loop through the database result set, parsing the template as we go.

// Insert <option>'s into categories <select>                    
for( $i=0; $i< count($categoryArray); $i++ ) {                    
   // If category not published, continue                    
   if ( $categoryArray[$i]->published() == 0 )                    
       continue;                    
                   
   // Set template variables for this heading                    
   if ( $categoryArray[$i]->category() != "" ) {                    
       if ( $categoryArray[$i]->id() == $categoryID )                    
           $tpl->set_var( "category_list_selected", "selected" );                    
       else                    
           $tpl->set_var( "category_list_selected", "" );                    
       $tpl->set_var( "category_list_id", $categoryArray[$i]->id() );                    
       $tpl->set_var( "category_list_category",                      
         $categoryArray[$i]->category() );                    
   } else {                    
       $tpl->set_var( "category_list_id", "" );                    
       $tpl->set_var( "category_list_category", "&nbsp;" );                    
   }                    
                   
   // Parse the template and append to the "template buffer"                    
   $tpl->parse( "category_list", "category_list_tpl", true );                    
}

So we've looped through the array, assigning template variables as we go, and finishing each loop by adding a parsed copy of the template block to the "template buffer". Note that we haven't actually displayed anything to our users yet.

$tpl->set_var( "question", $faq->Question );                    
$answer=eZTextTool::nl2br( $faq->Answer, true );                    
$tpl->set_var( "answer", $answer );

Here we assign a FAQ Question and answer to template variables, taking advantage of our eZFaq ACCESSORS. Notice the use of eZTextTool to convert new lines found in the fact to XHTML <br /> tags.

// Output the "template buffer" and print                    
$tpl->pparse( "output", "faqview_tpl" );                    
?>

Finally we use the ezTemplate pparse() method to parse and print the final template -- our users have their interface!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links