Creating custom template (operators of functions)

Author Message

Glenn MacGregor

Friday 14 November 2003 8:45:38 am

Hello,

I am trying to create a random template operator (or function not sure which) that mimics the PHP rand function. I am using ezp3.2-3, looking at the code and the example Custom template operators they don't match. So I found in the code that template.ini can define the mapping to PHP functions to template functions (or operators). So I added this line to the template.ini file:

PHPOperatorList[random_number]=rand

turned on debug, added the following line to a template file:

{let var=23}
{$var}
{$var|random_number}
{/let}

The debug gives me the following message:

Warning: PHP Nov 14 2003 15:52:22 rand() expects exactly 2 parameters, 1 given in /var/www/html/ezpublish-3.2-3/lib/eztemplate/classes/eztemplatephpoperator.php on line 86

So I tried the following line in my template file:

{let var=45}
{$var}
{random_number(0,$var}
{/let}

This produces the exact same debug output.

So it seems that functions which can be mapped in the template.ini file are PHP functions that take only 1 parameter.

How do I map a function that takes more than 1 parameter?

Thanks

Nicklas Lundgren

Friday 14 November 2003 9:12:24 am

Hi,
Im not sure that you can make a template operator that takes more than one param.
However I used the php function shuffle(array) instead of rand. Since it only takes one param its easy to make the template operator.

Shuffle is good for instance if you want to select a random article. Then you can fetch articles to an array and run your template operator shuffle on it, like:

$myArticle=$arrMyArticles|shuffle

This will give you one of the articles (randomly selected).

Of course, this only replaces the need for random numbers in certain cases.

Anyhow, hope this helps.

Regards,
Nicklas

Paul Borgermans

Friday 14 November 2003 11:22:28 am

Indeed, the template.ini operators can only map to functions that take 1 argument. If you need more, you will have to write a new template operator as an extension. It is not so difficult: take a look at the contributions and the RAD tool in the admin interface.

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.