Improved template handling with exceptions

The Page class is now responsible for executing the subpage template
rather than the index file.
Template processing has been improved with exceptions to catch 404s, or
any other template exception, which will abort the current page and
display an error document instead.
This commit is contained in:
2010-03-21 00:21:04 +00:00
parent 42268af1c7
commit 611182d09e
9 changed files with 102 additions and 14 deletions

View File

@@ -10,4 +10,8 @@ class HandBrakeCluster_Exception_ResultCountMismatch extends Exception {};
class HandBrakeCluster_Exception_UnknownSetting extends Exception {};
class HandBrakeCluster_Exception_TemplateException extends Exception {};
class HandBrakeCluster_Exception_Unauthorized extends HandBrakeCluster_Exception_TemplateException {};
class HandBrakeCluster_Exception_FileNotFound extends HandBrakeCluster_Exception_TemplateException {};
?>

View File

@@ -29,7 +29,7 @@ class HandBrakeCluster_Main {
$this->smarty->config_fir = './config';
$this->smarty->assign('version', '0.1');
$this->smarty->assign('base_uri', '/handbrake/');
$this->smarty->assign('base_uri', dirname($_SERVER['SCRIPT_NAME']) . '/');
}

View File

@@ -18,22 +18,43 @@ class HandBrakeCluster_Page {
}
public function template_filename() {
$template_filename = $this->page . '.tpl';
if (!$this->smarty->template_exists($template_filename)) {
$template_filename = 'home.tpl';
}
return $template_filename;
return $this->page . '.tpl';
}
public function evaluate() {
$code_filename = 'pages/' . $this->page . '.php';
public function evaluate($template_variables = array()) {
$code_filename = $this->page . '.php';
$template_filename = $this->template_filename();
if (file_exists($code_filename)) {
eval("include '$code_filename';");
try {
$this->render($template_filename, $code_filename, $template_variables);
} catch (HandBrakeCluster_Exception_FileNotFound $e) {
$this->render('errors/404.tpl', 'errors/404.php');
} catch (HandBrakeCluster_Exception_TemplateException $e) {
$this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array(
'exception' => $e,
));
}
}
protected function render($template_filename, $code_filename = null, $template_variables = array()) {
if ( ! $this->smarty->template_exists($template_filename)) {
throw new HandBrakeCluster_Exception_FileNotFound($template_filename);
}
// Copy all the template variables into the namespace for this function,
// so that they are readily available to the template
foreach ($template_variables as $__k => $__v) {
$$__k = $__v;
}
// Include the template code file, which will do all the work for this page
$real_code_filename = 'pages/' . $code_filename;
if ($code_filename && file_exists($real_code_filename)) {
include $real_code_filename;
}
// Now execute the template itself, which will render the results of the code file
$this->smarty->assign('page_content', $this->smarty->fetch($template_filename));
}
};

View File

@@ -42,6 +42,10 @@ class HandBrakeCluster_RequestParser {
return null;
}
public function request_string() {
return $this->request_string;
}
};