// Decide which layout to use
$oo->layout = ($oo->layout != "") ? $oo->layout : $oo->config->get("DefaultLayout", OO_DEFAULT_LAYOUT);
$layout_file = sprintf("%s/%s.html", $layout_path, $oo->layout);
// Load the page
$oo->page = ($oo->page != "") ? $oo->page : $oo->action;
$page_file = sprintf("%s/%s.html", $page_path, $oo->page);
if (file_exists($page_file)) {
ob_start();
include $page_file;
$oo->page_content = ob_get_clean();
}
else {
throw new Exception("The page file '$page_file' does not exist.");
}
// Load the layout
if (file_exists($layout_file)) {
ob_start();
include $layout_file;
$oo->layout_content = ob_get_clean();
}
else {
throw new Exception("The layout file '$layout_file' doesn't exist.");
}
// Finally render the whole thing
echo $oo->layout_content;
}
views:
135answers:
1This code contains an object, but this code is not an example of objects in PHP. That being said, we'll look at a couple portions of this that you might find confusing:
$oo->layout = ($oo->layout != "")
? $oo->layout
: $oo->config->get("DefaultLayout", OO_DEFAULT_LAYOUT);
This is known as the ternary operator, and is basically a form of the if-statement, or conditional-logic in general. In its most simplest form, it looks like this:
(condition) ? what to do if true : what to do if false ;
So the original line could be re-written as:
if ($oo->layout != "")
$oo->layout = $oo->layout; /* Kinda silly, huh */
else
$oo->layout = $oo->config->get("DefaultLayout", OO_DEFAULT_LAYOUT);
That should really be rewritten to something like:
if (empty($oo->layout))
$oo->layout = $oo->config->get("DefaultLayout", OO_DEFAULT_LAYOUT);
As for this line:
$layout_file = sprintf("%s/%s.html", $layout_path, $oo->layout);
We're just looking at a fancy way of building a string. Notice the %s within the string. These represent values that will come as parameters later. So the first %s represents $layout_path, and the second %s represents $oo->layout.
You could rewrite this line as:
$layout_file = $layout_path . "/" . $oo->layout;
That's pretty much it. The rest of your code is pretty simple and shouldn't be confusing.