Skip to content

Commit

Permalink
Add an exception for an unmatched template
Browse files Browse the repository at this point in the history
  • Loading branch information
kolber committed Dec 13, 2009
1 parent 20b16c4 commit fd5e850
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 3 additions & 1 deletion app/asset-types/page.inc.php
Expand Up @@ -47,7 +47,9 @@ function template_name() {

function template_file() {
$template_file = glob('./templates/'.$this->template_name.'.{html,json,atom,rss,rdf,xml,txt}', GLOB_BRACE);
return !empty($template_file) ? $template_file[0] : false;
# error out if template file doesn't exist
if(empty($template_file)) throw new Exception('A template named \''.$this->template_name.'\' could not be found in the /templates folder.');
return $template_file[0];
}

}
Expand Down
5 changes: 1 addition & 4 deletions app/stacey.inc.php
Expand Up @@ -102,17 +102,14 @@ function __construct($get) {
$file_path = Helpers::url_to_file_path($route);

# return a 404 if a matching folder doesn't exist
if(!file_exists($file_path)) throw new Exception('404. Page does not exist.');
if(!file_exists($file_path)) throw new Exception('404');

# register global for the path to the page which is currently being loaded
global $current_page_file_path;
$current_page_file_path = $file_path;

# create new page object
$page = new Page($route);

# return a 404 if a matching template doesn't exist
if(!file_exists($page->template_file)) throw new Exception('404. Page does not exist.');

# render page
$this->render($page);
Expand Down
14 changes: 8 additions & 6 deletions index.php
Expand Up @@ -18,12 +18,14 @@

} catch(Exception $e) {

# return 404
header('HTTP/1.0 404 Not Found');
# if there is a 404 page set, use it
if(file_exists('./public/404.html')) echo file_get_contents('./public/404.html');
# otherwise, use this text as the default
else echo '<h1>404</h1><h2>Page could not be found.</h2><p>Unfortunately, the page you were looking for does not exist here.</p>';
if($e->getMessage() == "404") {
# return 404 headers
header('HTTP/1.0 404 Not Found');
if(file_exists('./public/404.html')) echo file_get_contents('./public/404.html');
else echo '<h1>404</h1><h2>Page could not be found.</h2><p>Unfortunately, the page you were looking for does not exist here.</p>';
} else {
echo '<h3>'.$e->getMessage().'</h3>';
}

}

Expand Down

0 comments on commit fd5e850

Please sign in to comment.