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?
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.
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!
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
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.