Caution: This documentation is for eZ Publish legacy, from version 3.x to 5.x.
For 5.x documentation covering Platform see eZ Documentation Center, for difference between legacy and Platform see 5.x Architecture overview.

Template operators

Another way to access functions in your extensions is to use template operators. While eZ Publish contains many template operators, we will define a new template operator called "result_type" with a $result_type parameter.

We will use the parameter to control the amount of data that is shown in our database table. The template command {jac('list')} shows an array of data and {jac('count')} shows the amount of data. We will use the same functions as in the template fetch example: fetch('modul1', 'list' , ...) and fetch( 'modul1', 'count', ... )

The definition of the existing template operators in the jacextension is done in the file extension/jacextension/autoloads/ eztemplateautoload.php (see Listing 18). The template operator’s actions are defined in a custom PHP class (in our case, in JACOperator of the file extension/jacextension/autoloads/ jacoperator.php) (see Listing 19).

<?php
 
// Which operators will load automatically?
$eZTemplateOperatorArray = array();
 
// Operator: jacdata
$eZTemplateOperatorArray[] = array( 'class' => 'JACOperator',
                            'operator_names' => array( 'jac' ) );
?>

Listing 18. extension/jacextension/autoloads/eztemplateautoload.php

<?php
/**
 * Operator: jac('list') and jac('count') <br>
 * Count: {jac('count')} <br>
 * Liste: {jac('list')|attribute(show)}
 */
class JACOperator
{
    public $Operators;
 
    public function __construct( $name = 'jac' )
    {
        $this->Operators = array( $name );
    }
 
    /**
     * Returns the template operators.
     * @return array
     */
    function operatorList()
    {
        return $this->Operators;
    }
 
    /**
     * Returns true to tell the template engine that the parameter list
     * exists per operator type.
     */
    public function namedParameterPerOperator()
    {
        return true;
    }
 
    /**
     * @see eZTemplateOperator::namedParameterList
     **/
    public function namedParameterList()
    {
        return array( 'jac' => array( 'result_type' => array( 'type' => 'string',
                                                     'required' => true,
                                                     'default' => 'list' ))
                      );
    }
 
    /**
     * Depending of the parameters that have been transmitted, fetch objects JACExtensionData
     * {jac('list)} or count data {jac('count')}
     */
     public function modify( $tpl, $operatorName, $operatorParameters,  $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters  )
    {
        $result_type = $namedParameters['result_type'];
        if( $result_type == 'list')
            $operatorValue = JACExtensionData::fetchList(true);
        else if( $result_type == 'count')
            $operatorValue = JACExtensionData::getListCount();
    }
}
?>

Listing 19. extension/jacextension/autoloads/jacoperator.php
And of course, do not forget to regenerate the autoloads :).

To tell eZ Publish that jacextension contains template operators, we have to define it in the eZ Publish configuration file extension/jacextension/settings/site.ini.append.php with ExtensionAutoloadPath[]=jacextension (see Listing 20).

<?php /* #?ini charset="utf-8"?
...
# search for template operators in jaceextension
[TemplateSettings]
ExtensionAutoloadPath[]=jacextension
*/ ?>

Listing 20. extension/jacextension/settings/site.ini.append.php

To check our template fetch functions fetch('modul1', 'list', hash('as_object', true() ) ) and fetch( 'modul1', 'count', hash() ) of the template operator jac('list') or jac('count') we extend the template list.tpl of the view list (see Listing 21) .

<h2>Template Operator: jac('count') and jac('list')</h2>
Count: {jac( 'count' )} <br />
List: {jac( 'list' )|attribute( 'show' )}
<hr />
 
<h2>Template Fetch Functions:
fetch( 'modul1','count', hash() )<br /><br />
fetch( 'modul1','list', hash( 'as_object', true() ) )</h2>
Count: {fetch( 'modul1', 'count', hash() )} <br>
List: {fetch( 'modul1', 'list', hash( 'as_object', true() ) )|attribute( 'show' )}

Listing 21. Testing the custom Template Fetch functions and the template operator - extension/jacextension/design/standard/templates/modul1/list.tpl

We open the view by, for example, accessing the URL http://localhost/ez/index.php/plain_site/modul1/list/tableblue/1234

Apart from the example array $data_array, it must show the data of the database table jacextension_data twice, once with the template operators and another time with the template fetch functions. This shows that there are different options for accessing the same functions in your extension in a template.