Forums / Cotonti / Support / PHP code in .tpl

how to insert php code into tpl files

duck101
#1 2012-07-27 16:55

Hello, I tried to insert a php code into a tpl file, and for some reason not all of the code would display right. I've heard that php in tpl files is a big no,no. I then tried another option-- <script src="form.php"></script> to no avail.  Is there another way to call up a php file from a tpl file?

GHengeveld
#2 2012-07-27 17:14

The only way to execute php code directly from a template file is to use a callback function. You can call any php function from your tpl, both built-in and custom functions. The return value will be displayed in the HTML, which means the callback should return a string or a number, or nothing at all. You can use a callback function like this:

{PHP|my_function('param1', 'param2')}

Alternatively, you can combine it with a template tag or php variable:

{MYTAG|my_function()}

{PHP.somevariable|my_function()}

More info: http://www.cotonti.com/docs/ext/themes/cotemplate_advanced

duck101
#3 2012-07-27 18:52

Sorry for my ignorance on callback functions, but how do I tell it where to find my php file. is the code below right?

{PHP.somevariable|my_function('form.php')}

 

Trustmaster
#4 2012-07-27 19:45

There is no way to include PHP files from a template and there will not be for security reasons. If you need a way to include the output of a PHP script in your template, then you should create a plugin that would wrap that PHP script and assign TPL tags, or at least assign its output to some global variable. You can place the PHP code directly into 'themes/your_theme/your_theme.php' file but still it must assign the HTML output to a global variable rather than echoing it.

May the Source be with you!