Login / Status
developer.Resource
Home . Documentation . Document Library . Core Documentation
Sponsors
hosted by punkt.deTYPO3 and Open Source MagazineAOE Media

8.3. Example: Userdefined tags

I just thought of a great example of an include script. Really useful. On the www.typo3.com website I sometimes list TypoScript code. That's done with a special tag, I introduced there. In order to try this, we'll stick to the original testsite, because it features some content which come in handy here.

1) Open the testsite template and add this:

# Add the custom tag, <TS>...</TS> to the parseFunc of bodytext
tt_content.text.20.parseFunc.tags {
  ts.stripNL = 0
  ts = PHP_SCRIPT 
  ts.file = fileadmin/tsbyex/testtag.inc
}

2) Open the document fileadmin/tsbyex/testtag.inc with your favourite text-browser and verify that this is the content:

<?
$contentOfTag = $this->getCurrentVal();
$content = '<B>'.$contentOfTag.'</b>';
?>

3) Open "Startpage" on the testsite and add <ts>...</ts> tags like you see here:

... and guess what. This is the outcome:

Moving a bit further, lets include a library with a class. By time that's a necessity anyway, so...

1) Add/modify this to the TypoScript template:

# Add the custom tag, <TS>...</TS> to the parseFunc of bodytext
tt_content.text.20.parseFunc.tags {
  ts.stripNL = 0
  ts = PHP_SCRIPT 
  ts.file = fileadmin/tsbyex/testtag2.inc
}

includeLibs.testclass_example = fileadmin/tsbyex/testclass.inc

2) Open the document fileadmin/tsbyex/testtag2.inc and fileadmin/tsbyex/testclass.inc with your favourite text-browser and verify that this is the content:

fileadmin/tsbyex/testtag2.inc

<?
$test_object = new testClass;
$contentOfTag = $this->getCurrentVal();
$content = $test_object->formatTS($contentOfTag, 1);
?>

fileadmin/tsbyex/testclass.inc

<?
class testClass {
function formatTS($input, $ln){
$input = ereg_replace("^[^".chr(10)."]*.","",$input);
$input = chop($input);
$cArr = explode(chr(10),$input);
reset($cArr);
$n = ceil(log10(count($cArr)));
$lineNum="";
while(list($k,$v)=each($cArr)){
if ($ln)$lineNum = sprintf("% ".$n."d",($k+1)).":   ";
$v=htmlspecialchars($v);
$cArr[$k] = str_replace(" ",chr(160),$lineNum.$v);
}
$output = implode($cArr, "<BR>")."<BR>";
$output = '<font face=verdana size=1 color=maroon>'.$output.'</font>';
return $output;
}
}
?>

3) Finally enter this to the content record:

... and you should see this in your frontend, when you reload:

Challenge (PHP_SCRIPT1)

Write a script that displays two formfields, lets you enter two numbers and when they are sent to the same page, you calculate the sum of the figures. And display it.

Challenge (PHP_SCRIPT2)

Find out what the function enableFields() from the class cObj (tslib/content.php) does. (The actual function is in t3lib/page.php though). What arguments does it take and how could you use this effectively?



TYPO3 Core API

TSRef

TYPO3 Coding Guidelines