Path

ez.no / ezpublish / documentation / development / kernel / custom template operators


Custom template operators

These documentation pages are no longer maintained. Please visit the new documentation site.

eztemplatephpoperator allows for built-in php functions to be added as operators to the templating system. This is a brief explanation of what I have figured out about this system. There are currently a number of limitations but I'm sure this functionallity will be extended in the future.

If you want to use any PHP function as an operator, then read this page: Template operator

Defining new operators

New template operators can be added easily by overiding the template.ini file. As an example we'd like to include base64 encoding as an operator. This is done with the following changes in template.ini

[PHP]

# A list with php functions and their equivelant template operator names

# Giving the operators a different name from the PHP functions

# are recommended, that way you ensure that all operators follow

# the same naming guidelines. It also means that you can change the

# php function later on without doing template changes.

PHPOperatorList[]

PHPOperatorList[upcase]=strtoupper

PHPOperatorList[downcase]=strtolower

PHPOperatorList[reverse]=strrev

PHPOperatorList[nl2br]=nl2br

PHPOperatorList[urlencode]=urlencode

PHPOperatorList[encodestring]=base64_encode

PHPOperatorList[decodestring]=base64_decode

You can see from the example that the two lines for base64 encoding and decoding were appended to this section. Cache was deleted and with the operator in place the string was encoded.

Tip: You'll notice I used encodestring and decodestring instead of the actual name of the function used. This allows me to be able to change the ini later should I choose another method of encoding without having to change template files.

Using new operators in templates

To use the new operator in your templates just add

{text you want to modify|new_ezpublish_operator}
to your templates

Example

{$node.name|upcasefirst}

Notes/Limitations

I discovered this by trying to add a random templates operator. Unfortunately I was not able to accomplish that task using this method. This was because it is only possible to use this feature with php functions that take a single parameter.

You must use the operator in the method described above

{upcasefirst($node.name)} - does not work.

Comments

Calling any PHP function (also user defined ones)

Hi Francisco,

See the link I have added at the top of the page, which describes a clean way to call PHP code from a template.

Solution Found

I have checked Mark Overduin contribution http://ez.no/developer/ez_publish...cks/including_php_files_in_templates (Thanls a lot Mark) and I will use a variant of it to preserve contents and design separation.

I will implement one function for every external funcionality I need and I will call it as a custom operator template, this will reduce the security risk associated with including custom code by the designers (a possibility with Marks approach).

Expanding posibilities

Great Job. Would this mean that if somebody expands PHP recompiling it we could have any type of php funcionality, like a banner ad system already developed?.

For example making and compiling PHP with a function that calls a poll from the poll system (apart from EZPublish) would allow us to use this function and pass the poll we want as a parameter:

{newest|new_ezpublish_poll_operator}

The function would receive the "newest" parameter or an integer for a specific poll, then the function would return the html that renders the poll.

Of course recompiling PHP is a big disadvantage, but it seems possible.

What i am looking for is how to insert php code in a template in order to call funcionality provided by third party programs. I inserted php code directly in a template and it come as is in the html code.

Any help will be appreciated.

Fantastic !

What a find ! Great job m8 !

Contents

Development

Extensions
eZ publish datamodel
eZ publish tuning and stability
Importing attribute data
Kernel
    Executing PHP shell scripts
    eZ SOAP Web Services Overview
    Deploying SOAP Servers with eZ Publis...
    Custom template operators
    Cache block optimization
Libraries
Scripting
Standards
System overview
Test Suite
Using Doxygen to create API documenta...


Created

19/05/2003
1:49:40 am
by Bruce Morrison

Last updated

20/04/2004
7:27:53 pm
by Gunnstein Lye

Authors

Bruce Morrison
Bård Farstad
Jason M
Gunnstein Lye



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.