Share » Forums » Developer » Template

Template

Template

Tuesday 28 December 2010 10:54:22 pm - 7 replies

Author Message

Ivo Lukac

Wednesday 29 December 2010 1:27:27 am

Directly no.

You can easily create custom template operators which are basically wrapped php code

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

namita varshney

Wednesday 29 December 2010 5:45:29 am

I had just started working on eZ publish,

So please can you say me how custom template operator is created?

Thanks

Namita

Ivo Lukac

Wednesday 29 December 2010 7:15:18 am

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

Ivo Lukac

Wednesday 29 December 2010 7:22:56 am

I forgot that there is a wizard for getting this kind of skeleton.

Go to Setup->RAD->Template operator wizard

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

namita varshney

Wednesday 29 December 2010 11:18:38 pm

Hello,

I have done all this process for creating the extension.

But its not working. I had called the operator from the template file but its not returning any value.

Can you say what i should do now?

Thanks

Namita

Ivo Lukac

Thursday 30 December 2010 3:37:20 am

Hm,

You enabled the extension, regenerated autoloads and cleared the cache?

Any errors in debug output?

ps

Just saw a typo in my previous post: Extension folder is 'extension', not 'extensions'

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

namita varshney

Thursday 30 December 2010 9:47:40 pm

Thanks Ivo Lukac

Its working Fine. Thanks for Your help.

Namita

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu