Click to See Complete Forum and Search --> : Question about conditionally including a file


aaronbdavis
02-07-2006, 09:21 AM
I am working on a page template class based on an article I found about PHP Templates with Heredocs.

the template file looks like this:$template =<<<EOD
<html>
<head>
<title>PAGE_TITLE</title>
</head>
<body>
PAGE_CONTENT
</body>
</html>
EOD;
I can get it to work, but I want to allow for some automation.
the template string will be in a file called template.php, and I want the class to try to load it from 1 of 3 places
current folder
failing that, doc_root/includes
failing that, a default version defined inside the class
my idea is the define the default one first, then try to include the one from the includes folder, then the one from the current folder.

Will this work and/or is there a better way to do it

bfsog
02-24-2006, 05:33 AM
<?php

if(file_exists("template.php"))
{
include("template.php");
} elseif(file_exists("doc_root/includes/template.php"))
{
include("doc_root/includes/template.php");
} else {
include($default);
}
?>

bokeh
02-24-2006, 08:42 AM
That is madness if you don't mind me pointing it out. Either refer to the file with an absolute path or a dynamic absolute path. If you are presently using a relative path in the class file and this is what is causing trouble it is quite simple to work out where in the file system the class file is located. Can you provide more info?

NogDog
02-24-2006, 11:31 AM
$path = ini_get('include_path');
ini_set('include_path', ".;{$_SERVER['DOCUMENT_ROOT']}/include;{$someClass->path}");
include "filename.php";
ini_set('include_path', $path); // set it back to what it was

aaronbdavis
02-24-2006, 11:53 AM
funny how long a post can sit around un-answered... and funny how much traffic it can get after the first reply...

Anyway, thanks guys, but i actually figured it out about a week ago.

bokeh
02-24-2006, 11:58 AM
funny how long a post can sit around un-answered... and funny how much traffic it can get after the first reply...

Anyway, thanks guys, but i actually figured it out about a week ago.
What was the solution/problem?

aaronbdavis
02-24-2006, 12:16 PM
the solution.

I just used an if(file_exists($file)) block for each possibility.

bokeh
02-24-2006, 12:34 PM
Ok, but are there really 3 files or is it a path issue?

aaronbdavis
02-24-2006, 12:40 PM
The name of the file to find is passed in, or defaults to template.html, and the class determines where to find the file. The template files are now plain text/html files. The $place_holders variable is an associative array, where the key is the text to replace in the template and the value is the text to replace with.

Here is the actual code for the constructor of my Page class, so you can see how it works. (Note: the __toString method is what actually makes the replacements. function __construct($place_holders, $template = 'template.html') {

$root = $_SERVER['DOCUMENT_ROOT'];

// detemine the appropriate template to use
// 1. Template file in the current directory
// 2. Template file in the include directory
// 3. Template default
if (file_exists($template)) {
$this->mPage = file_get_contents($template);
} elseif (file_exists("$root/includes/$template")) {
$this->mPage = file_get_contents("$root/includes/$template");
} else {
$this->mPage = "<html><head><title>ERROR: No Template Defined</ti".
"tle></head><body>Please define a template before ".
"you use this class</body></html>";
} // end if-elseif-else block
$this->mPlaceHolders = (is_array($place_holders)) ? $place_holders : array();
} // end constructor