CCAPS

How To Get Started with PHP CCAPS

CCAPS, or Carefully-Controlled Alternative PHP Syntax, is a web template system alternative to template engines such as Smarty and CakePHP. CCAPS, pronounced Cee-Caps, is what PHP web developers can use to separate XHTML source from programming logic, yet glue the two together nicely without introducing spaghetti code.

Preference

Many developers prefer it to engines such as Smarty because:

Guidelines

PHP already has an Alternative PHP Syntax, but what CCAPS does are the following guidelines:

 <? if (TAB == 'main'): ?>
 <xhtml goes here>
 <? else: ?>
 <xhtml goes here>
 <? endif; ?>
 View::Assign('MYVAR','This is my test');
 View::Display('vMain.php');

...whereas the Display method automatically assumes vMain.php is in a subfolder called "views".

 <?php
 require_once('classes/View.php');
 if (empty($_POST['name'])) {
     View::Assign('ERRORMSG','No name was provided');
 }
 View::Display('vTest.php');
 <?php ?>
 <?= ERRORMSG ?>

...which would cause the word 'ERRORMSG' to appear on the page. The fix is this:

 <?php
 require_once('classes/View.php');
 $sErr = '';
 if (empty($_POST['name'])) {
     $sErr = 'No name was provided';
 }
 View::Assign('ERRORMSG',$sErr);
 View::Display('vTest.php');
 <?php ?>
 <?= ERRORMSG ?>

Installation

Most PHP installations already support CCAPS and do not require additional libraries to be installed.

Sample View Class

A sample View class could look like the following.

 <?php
 
 class View {
 
 const ENCODE_DEFAULT = 0;
 const ENCODE_HTML = 1;
 const ENCODE_URL = 2;
 
 function Assign($sVarName, $sVal, $nType = self::ENCODE_DEFAULT) {
     switch ($nType) {
         case self::ENCODE_HTML: // show the html
             $sVal = htmlentities($sVal);
             break;
         case self::ENCODE_URL: // prepare for urls
             $sVal = urlencode($sVal);
             break;
         default: // 0, default, as is, unaltered
             break;
         
     }
     $sVarName = strtoupper($sVarName);
     define($sVarName, $sVal);
 }
 
 function Display($sTemplateFile) {
     $sScriptName = $_SERVER['SCRIPT_NAME'];
     $sBaseName = basename($sScriptName);
     define('U',str_replace($sBaseName,'',$sScriptName));
 
    $sPath = dirname(__FILE__);
    $sPath = str_replace('classes','',$sPath);
    require_once($sPath . 'views/' . $sTemplateFile);
 }
 
 }

So, as an example, you could have a test.php which looks like:

 <?php
 require_once('classes/View.php');
 View::Assign('TEST','This is my test.');
 View::Display('vTest.php');

Then, your vTest.php might look like:

 <?php ?>
 <html>
 <head><title>Test</title></head>
 <body>
 <?= TEST ?>
 </body>
 </html>

Notice how we use TEST instead of $TEST. This is because it's a constant. This not only discourages abuses that could lead to spaghetti code, but also runs a lot faster than if we used a global variable array technique in our View class to load and unload variables as they are passed from one PHP page to another.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.