We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Embedding a PHP template in the header?

Development and Programming

Justin01's avatar
Justin01
4 posts
15 years ago
Justin01's avatar Justin01

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 35

So, 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.

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
15 years ago
Lisa Wess's avatar Lisa Wess

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?

       
Justin01's avatar
Justin01
4 posts
15 years ago
Justin01's avatar Justin01

I’m a little confused. Does this mean I can’t write my own PHP functions and include them in a template without the use of plug-ins? As for variables, are you referring to “embed”(?), because all I want is to call external PHP functions (and not PHP variables) inside my index.

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
15 years ago
Lisa Wess's avatar Lisa Wess

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.

       
Justin01's avatar
Justin01
4 posts
15 years ago
Justin01's avatar Justin01

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.

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
15 years ago
Lisa Wess's avatar Lisa Wess

I’m happy to help, Justin - I’m sorry I didn’t have a full solution for you. I don’t want to close this off as some of our community may have tackled it, so I’m going to move the thread down to Development & Programming for you.

       
Greg Aker's avatar
Greg Aker
6,022 posts
15 years ago
Greg Aker's avatar Greg Aker

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

       
Justin01's avatar
Justin01
4 posts
15 years ago
Justin01's avatar Justin01

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.

       
Greg Aker's avatar
Greg Aker
6,022 posts
15 years ago
Greg Aker's avatar Greg Aker

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

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.