Thursday, May 2, 2013

Sometimes You Don't Need an Extension

When I first started using TYPO3, I would write extensions for all of the extra functionality I needed. Eventually my extension list got quite long and some extensions I was writing were very simple and didn't really need all the overhead of the extension class. I started using PHP_SCRIPT_INT instead of creating new extensions, but this created another problem: now I had a bunch of include scripts! So I started grouping related scripts into one include script and using a key to determine which code in the script to run. This became even more useful when I made the key a typoscript object so I could use other things like GET/POST or session variables to determine which function in the include script to use. Here's an example of a simple include script:

<?php
if (!is_object($this)) die ('Error: No parent object present.');

$key = $this->cObjGetSingle($conf['key'],$conf['key.']);
switch ($key) {
  case "1":
  $content = 'case 1';
  break;

case "2":
  $content = 'case 2';
  break;

default:
  $content = 'default';
  break;

}
?>

You can then use the script in typoscript:

10 = PHP_SCRIPT_INT
  10 {
    file = fileadmin/my_include_script.inc
    key = TEXT
    key.data = GP:case
  }