Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,059 of 9,059 pages
About the help in other languages
All help pages

Writing template operators

Add your own template operators - the autoload registration file, the operator class with operatorList, namedParameterList and modify, piped and parameter input, escaping, and when an operator is the wrong tool.

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/help/creators/app-creators/template-operators.md

On this page

Writing template operators

This page is for application creators who give templates a new operator, such as {$price|expdemo_money( 'EUR' )}. An operator is a PHP class registered in the extension's autoloads/eztemplateautoload.php. The RAD tool Template extension (/setup/templateoperator) writes a working one for you (RAD tools).

Contents

  1. Registration
  2. The operator class
  3. Input and output
  4. Rules
  5. When not to write an operator

Registration

# extension/expdemo/settings/site.ini.append.php
<?php /* #?ini charset="utf-8"?
[TemplateSettings]
ExtensionAutoloadPath[]=expdemo
*/ ?>
<?php
// extension/expdemo/autoloads/eztemplateautoload.php
$eZTemplateOperatorArray = array();
$eZTemplateOperatorArray[] = array(
    'script' => 'extension/expdemo/autoloads/expdemotemplateoperators.php',
    'class' => 'expDemoTemplateOperators',
    'operator_names' => array( 'expdemo_money' ) );

The shipped ezstarrating extension registers its operators the same way (extension/ezstarrating/autoloads/eztemplateautoload.php).

The operator class

<?php
class expDemoTemplateOperators
{
    function operatorList()
    {
        return array( 'expdemo_money' );
    }

    function namedParameterPerOperator()
    {
        return true;
    }

    function namedParameterList()
    {
        return array( 'expdemo_money' => array(
            'currency' => array( 'type' => 'string', 'required' => false, 'default' => 'EUR' ) ) );
    }

    function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
    {
        switch ( $operatorName )
        {
            case 'expdemo_money':
                $operatorValue = number_format( (float)$operatorValue, 2 ) . ' ' . $namedParameters['currency'];
                break;
        }
    }
}

Input and output

  • The piped value ($price|expdemo_money) arrives in $operatorValue; set it to the result.
  • Named parameters arrive in $namedParameters, with their defaults applied.
  • An operator used without input ({expdemo_money( 12 )}) gets null as $operatorValue; declare the value as a parameter if that is how it is used.

Rules

  • Return values, not HTML: let the template print and wash them. If you must return HTML, escape every piece of input with htmlspecialchars().
  • Keep operators fast and side-effect free; they run on every render and inside cache blocks.
  • Prefix new operator names with your extension (expdemo_...) so they never collide.
  • Regenerate autoloads and clear the template caches after adding one.

When not to write an operator

NeedBetter
Read dataa fetch function (Writing your own fetch function)
String formattingthe shipped string operators (String template operators)
Permission checksRole and policy template operators
Markup reused in many templatesan {include} of a template part

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x