ez.no / ezpublish / documentation / development / extensions / template operator
These documentation pages are no longer maintained. Please visit the new documentation site.
With custom template operators you can call any PHP function from a template. This example explains how to create two operators, one with two parameters, and one without parameters. If you want to call a built-in PHP function, such as strrev(), then you probably want this page instead.
Create these directories:
extension/myextension
extension/myextension/autoloads
extension/myextension/settings
Create
extension/myextension/settings/site.ini.append
with the following content:
[TemplateSettings] ExtensionAutoloadPath[]=myextension
This suggests that eZ publish should look for autoload files in this directory.
Create
extension/myextension/autoloads/eztemplateautoload.php
with the following content:
<?php // Operator autoloading $eZTemplateOperatorArray = array(); $eZTemplateOperatorArray[] = array( 'script' => 'extension/myextension/autoloads/mystringoperators.php', 'class' => 'MyStringOperators', 'operator_names' => array( 'addstrings', 'helloworld' ) ); ?>
This tells eZ publish that we have two operators, which class they are in, and which file.
Create
extension/myextension/autoloads/mystringoperators.php
with the following content:
<?php class MyStringOperators { /*! Constructor */ function MyStringOperators() { $this->Operators = array( 'addstrings', 'helloworld' ); } /*! Returns the operators in this class. */ function &operatorList() { return $this->Operators; } /*! eturn true to tell the template engine that the parameter list exists per operator type, this is needed for operator classes that have multiple operators. */ function namedParameterPerOperator() { return true; } /*! The first operator has two parameters, the other has none. See eZTemplateOperator::namedParameterList() */ function namedParameterList() { return array( 'addstrings' => array( 'string1' => array( 'type' => 'string', 'required' => true, 'default' => '' ), 'string2' => array( 'type' => 'string', 'required' => true, 'default' => '' ) ), 'helloworld' => array() ); } /*! Executes the needed operator(s). Checks operator names, and calls the appropriate functions. */ function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$operatorValue, &$namedParameters ) { switch ( $operatorName ) { case 'addstrings': { $operatorValue = $this->addStrings( $namedParameters['string1'], $namedParameters['string2'] ); } break; case 'helloworld': { $operatorValue = $this->helloWorld(); } break; } } /*! eturn the sum of two strings. */ function addStrings( $string1, $string2 ) { return $string1 . $string2; } /*! eturn a famous string. */ function helloWorld() { return 'Hello World!'; } /// privatesection var $Operators; } ?>
This is the actual implementation of our two operators. The logic here connects the two template operators addstrings and helloworld with the PHP functions addStrings( $string1, $string2 ) and helloWorld().
Activate the extension. This page explains how to do it: Introduction to extensions
Now you should be ready to start using your new operators. Try this in a template:
<p>{addstrings( 'Forty', 'two' )}</p> <p>{helloworld()}</p>
You should see the output "Fortytwo" and "Hello World!".
Comments
custom template operator is not refreshed
dhimant jayswal
Thursday 14 December 2006 5:10:12 am
i have made 1 custom oprator that returns database table in string formate
but every time it returns the same string ,may be from cache
if i m clearing the cache then the real data from table returns
its not working
dhimant jayswal
Wednesday 13 December 2006 12:50:26 pm
like kernel/myfolder/
and add the parameters like
AutoloadPathList[]=kernel/myfolder/
and added line in my tpl which was overriden of node/view/full.tpl
as
{helloworld()}
still not working
I can't pass the parameter the normal way.
Martin Harispuru
Saturday 14 January 2006 3:58:59 pm
I have to do this instead : {operator(param)}
Why