Template

Author Message

namita varshney

Tuesday 28 December 2010 10:54:22 pm

Can we write PHP code in Template file?

Thanks

Namita

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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 30 2025 19:33:25
Script start
Timing: Jan 30 2025 19:33:25
Module start 'layout'
Timing: Jan 30 2025 19:33:25
Module start 'content'
Timing: Jan 30 2025 19:33:25
Module end 'content'
Timing: Jan 30 2025 19:33:25
Script end

Main resources:

Total runtime0.0250 sec
Peak memory usage8,192.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0047 588.0078151.1953
Module start 'layout' 0.00470.0042 739.2031220.6406
Module start 'content' 0.00890.0148 959.84381,005.9922
Module end 'content' 0.02370.0013 1,965.835941.9766
Script end 0.0250  2,007.8125 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002811.0066140.0002
Check MTime0.00135.0321140.0001
Mysql Total
Database connection0.00062.429810.0006
Mysqli_queries0.00239.004630.0008
Looping result0.00000.087710.0000
Template Total0.00093.610.0009
Template load0.00072.797610.0007
Template processing0.00020.834710.0002
Override
Cache load0.00051.974310.0005
General
dbfile0.00031.107280.0000
String conversion0.00000.032440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs