Hi,
I’m confronted to a problem I can’t seem to resolve even after reading every possible thread/documentation that has been posted on this forum about this subject.
All I’d like to do is to include a PHP template (that will contain only PHP functions) in my template’s header so I can retrieve any function I’d like in my header/index template.
I first created a template named “functions” in my template group, allowed PHP in this template and in header/index. All parsings are set to “Input”.
Here’s the code in “functions” that serves as a test:
<?php
function hello(){
echo "HELLO";
}
?>In my “header” template, I added before the DOCTYPE:
{embed="pp/functions"}Later, in my “index” template, I simply add
<?php hello(); ?>and it gives me this error:
Fatal error: Call to undefined function hello() in /home/mywebsite/eeapp/expressionengine/libraries/Functions.php(650) : eval()'d code on line 35So, since embeds seem out of the question, what should I do? Should I use a PHP include?
If I put this line before my DOCTYPE:
<?php include "{site_url}../ress/functions.php"; ?>My JS scripts, which are found and executed, are loaded from the same folder… but for some reason, when I load this PHP file, it shoots me the same error (same as above).
So my question is; how can I embed/include a PHP template so I can use its functions in my header/index template?
Thank you all in advance for any tips/snippets/solutions that may help me solve this problem.
Embedded templates are parsed as their own unit, you’d have to explicitly pass the variable through the embed. You can read about that in the user guide section on passing variables via embeds. You might consider using a plugin to pass your variables around, perhaps the string plugin might help? Or perhaps this Variables add-on might hit the mark?
You can use PHP in a template; but there isn’t a way to pass the variables that you make with PHP from one template to the next. I’m not a coder, so bear with me if I’m using the wrong terminology. What I mean is that you can’t have one template with your PHP functions, then call them in other templates. You’d need to put those functions into an add-on to use them throughout your site.
This answers the “embed” problem, although I’m a little dissapointed by the lack of this “functionality”.
Now I’ll have to look and see as to why using the PHP include function can apparently load the PHP file but not find/execute the functions when called in my templates.
Thank you.
Justin01,
I’d like to caution you if you do decide to go down this road. I’ve seen some sites try to do this, but unfortunately it can lead to unmaintainable code and real performance issues.
First off, PHP in your templates needs to be evaluated through the PHP eval() function. This can effectively double the page execution time. Being on top of CodeIgniter, you can have helpers, libraries, models in your packages that keep your code in one concise place. You can easily map these function calls to a plugin. eg:
{exp:plugin_class_name:method_name}So your desired way of accomplishing your goal is essentially the same.
If you’d like to see benchmarks, I have written about it here
-greg
Hey greg. Thanks for taking the time to respond.
Are you saying that I can include as many functions as I want in a single plug-in/class?
If yes, do you see this tutorial as being up to date with the API?
I’ll start writing my plug-in right away, but having the answers to those questions would undoubtedly help move things foward.
Thank you again.
The tutorial is for EE 1.x, so while the fundamentals are close, there are syntactical differences. I’d look that over, read the current Plugin Docs, and look over existing plugins to understand how they are made.
-greg
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.