|
Using a custom function is as easy as eating cheese cake. They can be used like the builtin tags tmpl, sub, var, etc. 01 <patTemplate:tmpl name="page"> 02 <div> 03 Today is 04 <patTemplate:time format="m/d/Y"/> 05 </div> 06 </patTemplate:tmpl> This will output Today is [current date]., where [current day] will be replaced with the current date. 01 <patTemplate:tmpl name="root"> 02 This is a template that is used to display code. 03 <patTemplate:phpHighlight><?PHP 04 $i = 0; 05 while( $i < 10 ) 06 { 07 echo "This is line $i.<br />"; 08 $i++; 09 } 10 ?> 11 </patTemplate:phpHighlight> 12 </patTemplate:tmpl> In the above example, the PHP code enclosed between the <patTemplate:phpHighlight> tags will be syntax highlighted in the output. Creating a custom Function Custom Functions have to be placed in patTemplate/Function and extended from patTemplate_Function. You have to implement one method that represents the function: string patTemplate_Function::call( array params, string content ) The reader will pass an associative array containing all attributes of your tag as well as a string as second parameter, which will contain all character data (and HTML tags) between the opening and closing tags. In this method you may compute the result and return it as a string. This result will then be inserted in the template instead of your function tag as well as the enclosed content. You may use all functions, that are located in the patTemplate/Function folder, as patTemplate will auto-load the classes. Template functions will be evaluated while analyzing the template, there's currently no way to use template variables or any input from the PHP script in the funcitons. Functions may be nested, patTemplate will always evaluate the innermost function first. Code examples This example is the code used for the custom function time, which is able to display the current time as well as reformat any timestamp you pass to this. 01 <?PHP 02 /** 03 * patTemplate function that calculates the current time 04 * or any other time and returns it in the specified format. 05 * 06 * @package patTemplate 07 * @subpackage Functions 08 * @author Stephan Schmidt <schst@php.net> 09 */ 10 class patTemplate_Function_Time extends patTemplate_Function 11 { 12 /** 13 * name of the function 14 * @access private 15 * @var string 16 */ 17 var $_name = 'Time'; 18 19 /** 20 * call the function 21 * 22 * @access public 23 * @param array parameters of the function (= attributes of the tag) 24 * @param string content of the tag 25 * @return string content to insert into the template 26 */ 27 function call( $params, $content ) 28 { 29 if( !empty( $content ) ) 30 { 31 $params['time'] = $content; 32 } 33 34 if( isset( $params['time'] ) ) 35 { 36 $params['time'] = strtotime( $params['time'] ); 37 } 38 else 39 { 40 $params['time'] = time(); 41 } 42 43 return date( $params['format'], $params['time'] ); 44 } 45 }?> The next code snippet is all that is needed for the phpHighlight function.
01 <?PHP 02 /** 03 * patTemplate function that highlights PHP code in your templates 04 * 05 * @package patTemplate 06 * @subpackage Functions 07 * @author Stephan Schmidt <schst@php.net> 08 */ 09 class patTemplate_Function_Phphighlight extends patTemplate_Function 10 { 11 /** 12 * name of the function 13 * @access private 14 * @var string 15 */ 16 var $_name = 'Phphighlight'; 17 18 /** 19 * call the function 20 * 21 * @access public 22 * @param array parameters of the function (= attributes of the tag) 23 * @param string content of the tag 24 * @return string content to insert into the template 25 */ 26 function call( $params, $content ) 27 { 28 ob_start(); 29 highlight_string( $content ); 30 $content = ob_get_contents(); 31 ob_end_clean(); 32 return $content; 33 } 34 } 35 ?>
|