Forums / Developer / PHP code in templates

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 :-)

eZ debug

Timing: Jan 18 2025 15:16:02
Script start
Timing: Jan 18 2025 15:16:02
Module start 'content'
Timing: Jan 18 2025 15:16:02
Module end 'content'
Timing: Jan 18 2025 15:16:02
Script end

Main resources:

Total runtime0.1519 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0067 588.7266180.8438
Module start 'content' 0.00670.0056 769.5703101.8516
Module end 'content' 0.01230.1396 871.4219530.9844
Script end 0.1518  1,402.4063 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00362.3635200.0002
Check MTime0.00140.9509200.0001
Mysql Total
Database connection0.00090.606310.0009
Mysqli_queries0.109271.88591410.0008
Looping result0.00110.74001390.0000
Template Total0.139391.710.1393
Template load0.00080.541110.0008
Template processing0.138491.125210.1384
Override
Cache load0.00060.395710.0006
Sytem overhead
Fetch class attribute can translate value0.00070.470710.0007
XML
Image XML parsing0.00030.194110.0003
General
dbfile0.00362.3537200.0002
String conversion0.00000.002730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs