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