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

PHP require

Development and Programming

DeeCee's avatar
DeeCee
118 posts
16 years ago
DeeCee's avatar DeeCee

What is the correct format to embed a template in a php require? I have the files in a template group called php_includes with a template for each file to be included (and I do have php enabled for each template)

<?php
$db = mysql_connect("localhost","root","pw");
if (!$db) {
    die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("cwc_mysql",$db);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}
?>

<?php require_once({embed="php_includes/db_functions_php"}) ?>

I get: Parse error: parse error in C:\wamp\www\cwc\ee_cwc_system\core\core.functions.php(637) : eval()’d code on line 22

If I comment out the require stmt, it goes until it needs the function. I had the connect as a require and could not get it to work, so I embedded it and it works, so I think it is a format problem with embedding the template?

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete

Embeds are processed after the PHP of a template is evaluated so you cannot use an embed as a variable, but even if it were to work - embed takes a file name, not file contents. We do have some options though.

To get the behavior you’re after, the easiest approach is to put any extra PHP into flat files and include them directly. For example, let’s say we put them into a folder called system/cwc_includes.

Then you can include them using a full path:

<?php
$db = mysql_connect("localhost","root","pw");
if (!$db) {
    die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("cwc_mysql",$db);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}
?>

<?php require_once(PATH.'cwc_includes/db_functions.php'); ?>

However, my suggestion would be to move all the code into a plugin/module, and simply call that from within the template.

       
silenz's avatar
silenz
1,648 posts
16 years ago
silenz's avatar silenz

If you were to pursue your inital approach and have allow_url_fopen enabled in PHP you could probably make it work if you use a {path=}-variable instead of an {embed=}.

However Pascal’s suggestion seem more sensible.

       
DeeCee's avatar
DeeCee
118 posts
16 years ago
DeeCee's avatar DeeCee

Thank you! I have two questions:

1) what is PATH. with in php? is that an EE variable? what would it contain? is it the same value as {site_url}?

2) I have a post http://ellislab.com/forums/viewthread/119209/ on setting cookies to create breadcrumbs. Does the php version that I came up with look like something that I should move into a plugin/module? I have not done anything with my own plugins or modules yet.

Could you look at that post and let me know? I appreciate it!

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete
1) what is PATH. with in php? is that an EE variable? what would it contain? is it the same value as {site_url}?

It’s the server path to your system folder - not a url. PHP includes generally use local paths (they can use urls, but there are very few reasons to do that).

2) Responded in the other thread.

-Pascal

       

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.