Path

ez.no / ezpublish / documentation / customization / tips & tricks / including php files in templates


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)

This might be relevant for some. See this link:
http://ez.no/ez_publish/documenta...lopment/extensions/template_operator

Page hangs when PHP script is on same site

Hi,

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

Hi all,

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

Thanks a lot for your solution Mark, I am going to use it with a variant.

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

... rewrite the code in the original doc?

hmmm

The idea is good, but the implementation is crappy.

you should use "eval" for this and not make a 2nd request.

Contents

Customization

Access control
eZ publish API Documentation
Content structure
Custom design
Components
Tips & Tricks
    Debugging templates
    Javascript in templates
    Fetching current user
    Showing related objects
    Show which templates are used
    Fetching the ID of the parent
    One Article Folder
    Creating tree menus
    How can I use my own php script insid...
    Hiding attribute content
    Splitting an article over several pages
    Accessing section id in pagelayout
    List articles in folder
    Creating nice URLs
    Alt tag on images
    Improve the pagelayout.tpl
    Clean HTML tags
    Insert javascript call inside ezpub d...
    3rd party applications
    Fetch Function examples
    Display more than 15 items on your co...
    Including PHP files in templates
    Redirecting after content publishing
    HowTo see if article has an image
    Date and Time Formats
    Forms processing example: store user...
    Adjust Timezone
    Adding a Remove Button/Image
    Find the age in years for an object
    Editing, creating and removing conten...
    Indexing binary files with IFilters o...
Troubleshooting


Created

15/07/2003
12:13:15 pm
by Mark Overduin

Last updated

30/12/2003
3:00:11 pm
by Mark Overduin

Authors

Mark Overduin
Jan Borsodi
Bård Farstad



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.