ez.no / ezpublish / documentation / customization / tips & tricks / including php files in templates
These documentation pages are no longer maintained. Please visit the new documentation site.
Note: This is not a perfect solution, because you have to 'hack' a kernel file. If there's anyone out there who knows a way to include PHP files in templates, please post it
Many of you programmers out there want to use PHP in your templates. I've come up with a solution that might fit your needs.
I've written a simple function that does what I needed: I wanted to include php files in my templates, so I could use a hitcounter, banner/ad system, etc.
The insert function (you can put it somewhere in ezTemplateAutoLoad.php, which is located under /kernel/common/):
function marksPHPinsert ($file) { $file = fopen ($file, "r" ); while (!feof ($file)) { $output .= fgets ($file, 4096); } fclose ($file); return ($output); }
Edit ezTemplateAutoLoad.php some more (line 69, at least that was the line number I had to 'use'). You should see something like this:
Note: The script below is used with version 3.1 of ezPublish
$eZTemplateOperatorArray[] = array( 'script' => 'lib/eztemplate/classes/eztemplatephpoperator.php', 'class' => 'eZTemplatePHPOperator', 'class_parameter' => array( 'upcase' => 'strtoupper', 'downcase' => 'strtolower', 'reverse' => 'strrev', 'nl2br' => 'nl2br', 'ucfirst' => 'ucfirst', 'marksPHPinsert' => 'marksPHPinsert'), 'operator_names' => array( 'upcase', 'downcase', 'reverse', 'nl2br', 'ucfirst', 'marksPHPinsert' ) );
As you can see, I added a word (marksPHPinsert). In class_parameter I added the line 'marksPHPinsert' => 'marksPHPinsert'; Whenever 'marksPHPinsert' is used in your templates, it will refer to the insert function.
In operator_name you have to add 'insert' too, else it will not work. Operator_names contains the functions that can really be used in your templates.
Note: The script below is used with version 3.2 and 3.3 of ezPublish
include ("marksPHPfunctions.inc.php"); $eZTemplateOperatorArray[] = array( 'script' => 'lib/eztemplate/classes/eztemplatephpoperator.php', 'class' => 'eZTemplatePHPOperator', 'class_parameter' => array('marksPHPinsert' => 'marksPHPinsert'), 'operator_names' => array( 'marksPHPinsert'));
As you can see, I include a file called 'marksPHPfunction.inc.php'. This file is located under '/kernel/common/'. You should put all your functions in a file (not a kernel file!!), otherwise your kernel files will all be messed up.
Try to always keep your kernel files clean. This way you do not have to change much if you upgrade to another version.
Note: For those who have the latest development version you can simply edit template.ini and add the following line in the group PHP
PHPOperatorList[marksPHPinsert]=marksPHPinsert
Well, we've come to the part that you want to use the function. Simply put the following line in your template:
{"http://www.mydomain.com/myfile.php"|marksPHPinsert}
The path to the file you wish to include must be a url (http://www.mydomain.com/myfile.php) instead of a local path (e.g. /local/path/to/file.php), because the local file will not be parsed. It will just show unparsed php code (of course).
This is it. Hope this is of some use to you.If you have any questions or comments, you can post them here.
Good luck, Mark Overduin
Comments
Calling any PHP function (also user defined ones)
Gunnstein Lye
Tuesday 20 April 2004 7:38:59 pm
http://ez.no/ez_publish/documenta...lopment/extensions/template_operator
Page hangs when PHP script is on same site
Vivienne van Velzen
Thursday 08 April 2004 10:38:28 am
I tried this method to include my own PHP script in a page, and it seems to work fine, as long as the script is at a different site (I only tried a 'hello world' example).
When I place the script in the root-directory or a subdirectory on my EZ site, the page where I try to include the script takes forever to load (and doesn't, in the end). Does anyone know how I can fix this? I would like to keep all my files and scripts for a site together, and not scatter them across other sites.
TIA,
Vivienne
Problem after putting Insert in eztemplateautoload.php
Maarten Holland
Saturday 13 September 2003 9:26:39 pm
I'm trying to get the Insertfunction to work. Since I couldn't find the file 'eztemplateautoloader.php', I've used eztemplateautoload.php in kernel/common. Is that right?
But now I get the following error when opening the page where I've put in the code: {"http://192.168.0.100/index.php"|insert}
"Fatal error: Maximum execution time of 30 seconds exceeded in /srv/www/htdocs/cms/lib/ezutils/classes/ezdebug.php on line 194
Fatal error: eZ publish did not finish it's request
The execution of eZ publish was abruptly ended, the debug output is present below."
There is no further debug output presented. I'm using EZP 3-1.1 on a Pentium Pro with 64 MB (increased the maximum for PHP from 8 MB to 20 MB).
Does anyone know what could be wrong? Any help is greatly appreciated!
Maarten
Maintaining design on its own layer
Francisco Felix
Friday 25 July 2003 11:59:41 pm
I am making one function for every external funcionality I need to prevent malicious code from being executed and keep design on its own layer, as an example our polling system, wich thanks to you i can call now inside a template like this:
{1|showpoll}
Thus showing the rigth poll (1 in this case, but the poll system allows things like "newest" and more) where I need it. For now I will be showing the poll results on a pop-up window to prevent issues like keeping state of the poll in a template (show results if already vote or show poll?).
Any pointer on keeping state between template calls will be appreciated.
Could you please
Karsten Jennissen
Tuesday 15 July 2003 2:59:53 pm
hmmm
Björn Dieding@xrow.de
Tuesday 15 July 2003 2:37:59 pm
you should use "eval" for this and not make a 2nd request.