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.

eZ debug

Timing: Jan 18 2025 10:33:58
Script start
Timing: Jan 18 2025 10:33:58
Module start 'layout'
Timing: Jan 18 2025 10:33:58
Module start 'content'
Timing: Jan 18 2025 10:33:58
Module end 'content'
Timing: Jan 18 2025 10:33:58
Script end

Main resources:

Total runtime0.4420 sec
Peak memory usage4,096.0000 KB
Database Queries54

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0049 587.9141152.6250
Module start 'layout' 0.00490.0029 740.539139.4453
Module start 'content' 0.00780.4329 779.9844540.2500
Module end 'content' 0.44070.0013 1,320.234412.1641
Script end 0.4420  1,332.3984 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.7058160.0002
Check MTime0.00130.2875160.0001
Mysql Total
Database connection0.00080.174410.0008
Mysqli_queries0.392088.6827540.0073
Looping result0.00050.1134520.0000
Template Total0.411693.120.2058
Template load0.00220.500820.0011
Template processing0.409492.616420.2047
Template load and register function0.00010.023510.0001
states
state_id_array0.00080.187010.0008
state_identifier_array0.00100.235820.0005
Override
Cache load0.00190.4296330.0001
Sytem overhead
Fetch class attribute can translate value0.00060.142720.0003
Fetch class attribute name0.00140.322640.0004
XML
Image XML parsing0.00090.214020.0005
class_abstraction
Instantiating content class attribute0.00000.001640.0000
General
dbfile0.00080.1766240.0000
String conversion0.00000.001140.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
8content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
7content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 21
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs