Path

ez.no / ezpublish / documentation / development / extensions / template operator


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

hi ,again
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

i have created all the files in one folder inside kernel
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.

Hi i based my work on this code but i can't use my operator like this : {param|operator()}

I have to do this instead : {operator(param)}

Why

Contents

Development

Extensions
    Translation
    Introduction to extensions
    Building an eZ publish module
    Design extension
    Datatypes
    Workflow events
    Module
    Template operator
    Template function
eZ publish datamodel
eZ publish tuning and stability
Importing attribute data
Kernel
Libraries
Scripting
Standards
System overview
Test Suite
Using Doxygen to create API documenta...


Created

09/07/2003
4:19:27 pm
by Bård Farstad

Last updated

25/10/2004
9:40:18 pm
by Guglielmo Celata

Authors

Bård Farstad
Jan Borsodi
Gunnstein Lye
Guglielmo Celata



This page is part of the eZ Publish documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.