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:
@@ -10,4 +10,8 @@ class HandBrakeCluster_Exception_ResultCountMismatch extends Exception {};
|
|||||||
|
|
||||||
class HandBrakeCluster_Exception_UnknownSetting 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 {};
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class HandBrakeCluster_Main {
|
|||||||
$this->smarty->config_fir = './config';
|
$this->smarty->config_fir = './config';
|
||||||
|
|
||||||
$this->smarty->assign('version', '0.1');
|
$this->smarty->assign('version', '0.1');
|
||||||
$this->smarty->assign('base_uri', '/handbrake/');
|
$this->smarty->assign('base_uri', dirname($_SERVER['SCRIPT_NAME']) . '/');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,22 +18,43 @@ class HandBrakeCluster_Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function template_filename() {
|
public function template_filename() {
|
||||||
$template_filename = $this->page . '.tpl';
|
return $this->page . '.tpl';
|
||||||
|
|
||||||
if (!$this->smarty->template_exists($template_filename)) {
|
|
||||||
$template_filename = 'home.tpl';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $template_filename;
|
public function evaluate($template_variables = array()) {
|
||||||
|
$code_filename = $this->page . '.php';
|
||||||
|
$template_filename = $this->template_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,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function evaluate() {
|
protected function render($template_filename, $code_filename = null, $template_variables = array()) {
|
||||||
$code_filename = 'pages/' . $this->page . '.php';
|
if ( ! $this->smarty->template_exists($template_filename)) {
|
||||||
|
throw new HandBrakeCluster_Exception_FileNotFound($template_filename);
|
||||||
if (file_exists($code_filename)) {
|
|
||||||
eval("include '$code_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));
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -43,6 +43,10 @@ class HandBrakeCluster_RequestParser {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function request_string() {
|
||||||
|
return $this->request_string;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ try {
|
|||||||
$page = new HandBrakeCluster_Page($smarty, $main->request());
|
$page = new HandBrakeCluster_Page($smarty, $main->request());
|
||||||
$page->evaluate();
|
$page->evaluate();
|
||||||
|
|
||||||
$smarty->assign('page_content', $smarty->fetch($page->template_filename()));
|
|
||||||
|
|
||||||
$smarty->display('index.tpl');
|
$smarty->display('index.tpl');
|
||||||
} catch (HandBrakeCluster_Exception $e) {
|
} catch (HandBrakeCluster_Exception $e) {
|
||||||
die("Uncaught Exception: " . $e->getMessage());
|
die("Uncaught Exception: " . $e->getMessage());
|
||||||
|
|||||||
9
pages/errors/404.php
Normal file
9
pages/errors/404.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$main = HandBrakeCluster_Main::instance();
|
||||||
|
$req = $main->request();
|
||||||
|
|
||||||
|
$this->smarty->assign('requested_page', htmlspecialchars($req->request_string()));
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
10
pages/errors/unhandled-exception.php
Normal file
10
pages/errors/unhandled-exception.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$main = HandBrakeCluster_Main::instance();
|
||||||
|
$config = $main->config();
|
||||||
|
|
||||||
|
$this->smarty->assign('display_exceptions', $config->get('debug.display_exceptions'));
|
||||||
|
$this->smarty->assign('exception', $exception);
|
||||||
|
$this->smarty->assign('exception_type', get_class($exception));
|
||||||
|
|
||||||
|
?>
|
||||||
6
templates/errors/404.tpl
Normal file
6
templates/errors/404.tpl
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<h2>The requested page could not be found</h2>
|
||||||
|
<p>
|
||||||
|
The file you requested ({$requested_page}) could not be found.
|
||||||
|
If you typed in the address manually, check that you have spelled it correctly,
|
||||||
|
or if you followed a link, let us know and we'll look into it.
|
||||||
|
</p>
|
||||||
36
templates/errors/unhandled-exception.tpl
Normal file
36
templates/errors/unhandled-exception.tpl
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<h2>An unhandled error has occurred</h2>
|
||||||
|
<p>
|
||||||
|
There was a problem trying to complete the requested action. Please try again and if the problem persists, let us know.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{if $display_exceptions}
|
||||||
|
<p>
|
||||||
|
An unhandled exception was caught during the page template processing. The full details are shown below:
|
||||||
|
</p>
|
||||||
|
<table class="exception-details">
|
||||||
|
<colgroup id="header">
|
||||||
|
<col />
|
||||||
|
</colgroup>
|
||||||
|
<colgroup>
|
||||||
|
<col />
|
||||||
|
</colgroup>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<th>Exception</th>
|
||||||
|
<td>{$exception_type}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>File</th>
|
||||||
|
<td>{$exception->getFile()}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>Line</th>
|
||||||
|
<td>{$exception->getLine()}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>Message</th>
|
||||||
|
<td>{$exception->getMessage()}</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{/if}
|
||||||
Reference in New Issue
Block a user