PHP code in templates

Author Message

Trond Hjelmaas

Thursday 24 March 2005 3:01:50 am

Hei,

I would like to make my own php code and include in the pagelayout.tpl. The code will read some tables (created by me) in the database and create dropdown boxes with options.

I tried to include php code in template, it didn't work (the php code was still visible in the finished html file).

I tried to include a php-file in the template like this: {include uri="design:TESTIMPORT.PHP"}. Didn't work either.

This solution "hacks the kernel" and is not preferred (yet):
http://ez.no/ez_publish/documentation/customization/tips_tricks/including_php_files_in_templates

This page uses extensions to call "any" php function from a template (doesn't separate code from presentastion):
http://ez.no/ez_publish/documentation/customization/tips_tricks/including_php_files_in_templates

Does anyone know about other options?

Help very much appreciated :)

Gabriel Ambuehl

Thursday 24 March 2005 3:13:33 am

Create your own template operator(s). It's quite simple and pretty well documented in the the documentation.

Thinking of it, it actually should be possible to write a generic template operator you can pass PHP files to execute...

Visit http://triligon.org

Trond Hjelmaas

Thursday 24 March 2005 8:43:23 am

Thanks Gabriel,

this is my first time using Ez and sorry if I ask basic questions. I wrote a template operator that was really simple and it worked, but not quite as I hoped for.

This is the function:;

function include_my_php()
{
include("myFile.php");
}

It includes the php file but it gets included at the very beginning of the html file:

----start of html file-----
The content of myFile.php
<html>
...my other content.
</html>
----start of html file-----

I would like the file to be included "inside" the html code, exactly where I call include_my_php().

Would you have any ideas how to do that?

Thanks in advance!

Lazaro Ferreira

Thursday 24 March 2005 11:29:15 am

Hi Trond,

Template operators can be autoloaded, so if your operator include another php script, probably that's the problem the include output is there before your template code gets executed

Lazaro
http://www.mzbusiness.com

Nathan Sharp

Sunday 27 March 2005 12:15:50 pm

The other aspect is that your template operator should not "echo" its output to the client. It should return the result as a string and the .tpl language will include it as appropriate.

Trond Hjelmaas

Wednesday 06 April 2005 4:16:41 am

Nathan,

can you please help me a bit more with <i>"It should return the result as a string and the .tpl language will include it as appropriate."</i>

I understand your idea but cannot make it happen in ez.

Thanks again!

Nathan Sharp

Wednesday 06 April 2005 6:46:53 pm

Before I start, note that I have not really tried what you are doing. My understanding, which may be wrong, is the following:

<b>Wrong</b>

function calledFromTemplate()
{
   echo "<b>hello</b> dude!";
}

function calledFromTemplate2()
{
   ?>
   <b>hello</b> dude!
   <?php
}

<b>Right</b>

function calledFromTemplate()
{
   return "<b>hello</b> dude!";
}

function calledFromTemplate2()
{
   ob_start();
   echo "<b>hello</b> dude!";
   return ob_get_clean();
}

function calledFromTemplate3()
{
   ob_start();
   ?>
   <b>hello</b> dude!
   <?php
   return ob_get_clean();
}

And finally, what I am guessing you want to do:

function include_my_php()
{
   ob_start();
   include("myFile.php");
   return ob_get_clean();
}

I am not familiar enough to know if the following problems will give you trouble:
1) Your included file probably outputs <html> and <head> tags that will end up embedded inside the larger eZp document.
2) Your included file will have a much different environment than being called directly (with regards to current directory, $PHP_SELF, etc).
3) If your included file has links, they may not correspond to correct ezp urls. I have no idea how ezp deals with post operations either.

I have not tested any of these code snippets. YMMV. Good luck and have fun.

BTW, if you get it working, let me know, I may have a use for something similar :-)

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 16:16:21
Script start
Timing: Jan 18 2025 16:16:21
Module start 'layout'
Timing: Jan 18 2025 16:16:21
Module start 'content'
Timing: Jan 18 2025 16:16:22
Module end 'content'
Timing: Jan 18 2025 16:16:22
Script end

Main resources:

Total runtime0.7473 sec
Peak memory usage4,096.0000 KB
Database Queries73

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0043 589.0078152.6094
Module start 'layout' 0.00430.0026 741.617239.4141
Module start 'content' 0.00690.7392 781.0313620.6641
Module end 'content' 0.74610.0012 1,401.695316.1875
Script end 0.7472  1,417.8828 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.4350160.0002
Check MTime0.00130.1769160.0001
Mysql Total
Database connection0.00060.078010.0006
Mysqli_queries0.683291.4248730.0094
Looping result0.00080.1023710.0000
Template Total0.721396.520.3606
Template load0.00210.284920.0011
Template processing0.719196.233220.3596
Template load and register function0.00020.022510.0002
states
state_id_array0.00110.148110.0011
state_identifier_array0.00120.158320.0006
Override
Cache load0.00190.2603580.0000
Sytem overhead
Fetch class attribute can translate value0.00060.082440.0002
Fetch class attribute name0.00180.234970.0003
XML
Image XML parsing0.00050.068540.0001
class_abstraction
Instantiating content class attribute0.00000.005370.0000
General
dbfile0.00050.0707100.0001
String conversion0.00000.001040.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
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
14content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 31
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs