PHP execution in templates

Author Message

Jan Borsodi

Saturday 26 July 2003 3:52:23 am

From the many threads on how to execute php functions or scripts in templates it only seems right to provide a simpler way to do this than it is now. (At least for integrating old scripts to eZ publish)

Note: eZ publish will never support inline PHP in templates, the goal is to have a clear separation of code and design. And also to have better control over what is executed.

1.
Create a runscript template operator. It will take the scriptname as the first parameter and will look for the scriptname in a script repository. The rest of the operator parameters are optional arguments which are sent to the script.

Repositories are defined in a new .ini file and will also support extensions. This means that it's not possible execute arbitrary scripts/php-files in eZ publish, ie. it's safer.

The optional operator parameters can be defined to map to named php variables when the script is run. For instance the first optional parameter can be mapped to a variable called $name, etc.
The default behaviour is to have all optional parameters in a sequential array.

Any output of the script is returned from the operator as a single string. To catch the output the php functions ob_start(), ob_get_contents() and ob_end_clear() is used.

Example:
{runscript('ads.php',$node.id)}

It could also be possible to let runscript be a template function instead of an operator.

2.
Improve the eztemplatephpoperator operator. It was primarily meant to map simple modifying php functions to template operators. For example.
{$str|upcase}

I suggest adding support for sending all operator parameters as php function arguments. For this to happen the operator must be defined in the template.ini file to do so, if not the normal modifying behaviour is used.
For instance mapping php function rand to random operator.
{random(1,5)}

I also suggest a naming convention for these new operators so that they won't clash with new operators in future eZ publish releases.
The best way is perhaps to do automatic prefixing of operators, for instance prefix with php_.
{php_random(1,5)}
The prefix could be set in the template.ini file, so those who don't want it (and accept the consequences with future updates) can remove the prefix.

--
Amos

Documentation: http://ez.no/ez_publish/documentation
FAQ: http://ez.no/ez_publish/documentation/faq

Norman Leutner

Friday 28 November 2003 4:48:16 am

****************************
Create a runscript template operator
****************************

Could you please explain exactly how to create that new "runscript template operator" and where to place it.

thanks

Norman Leutner

Mit freundlichen Grüßen
Best regards

Norman Leutner

____________________________________________________________
eZ Publish Platinum Partner - http://www.all2e.com
http://ez.no/partners/worldwide_partners/all2e_gmbh

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