Forums / Developer / Template / Comment64672

Author Topic

Ivo Lukac

Wednesday 29 December 2010 7:15:18 am

Comment64672

Well,

If you are a php developer it should be easy:

  • create an extension ( e.g. extensions/foo )
  • create folder autoloads in your extension (extensions/foo/autoloads)
  • create file eztemplateautoload.php in that folder (extensions/foo/autoloads/eztemplateautoload.php)
<?php
$eZTemplateOperatorArray = array();
$eZTemplateOperatorArray[] = array( 'script' => 'extension/foo/autoloads/bar.php',
        'class' => 'FooBarOperator',        
        'operator_names' => array('foobar' ) );
?>
  • create the actual php file (extensions/foo/autoloads/bar.php)
<?php
class FooBarOperator {   
 function operatorList() { 
   return array( 'foobar' ); 
 }
 function namedParameterPerOperator() { 
   return true; 
 }
 function namedParameterList() {  
   return array( 'foobar' => array(
      'param1' => array( 'type' => 'string', 'required' => true, 'default' => '') ,
      'param2' => array( 'type' => 'array', 'required' => false, 'default' => array() ) ) );
 }
 function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, &$namedParameters ) {
   switch ( $operatorName ) { 
     case 'foobar':           
     {              
         $operatorValue = $this->fooBar( $namedParameters['param1'],$namedParameters['param2']  );           
     } break;       
   }   
 }      
 function fooBar($param1, $param2) { 
   return "Hello World"; 
 }
}
?>

So we mapped fooBar() function to the "foobar" operator so you can call it from template like this: {foobar('something')}

For finishing you need to enable the extension and regenerate autoloads to make it work.

http://www.linkedin.com/in/ivolukac
http://www.netgen.hr/eng/blog
http://twitter.com/ilukac