php - Make Twig fallback to a 404 page when template not found -


i'm using twig standalone template engine site. using simple router called macaw takes "pretty url" , tells twig render appropriate page so:

macaw::get('/(:any)', function($slug) {     echo $globals['twig']->render('@pages/'.$slug.'.html'); }); 

it works fine except if try load non-existent page horrible error like

fatal error: uncaught exception 'twig_error_loader' message 'unable find template "@pages/this-link-is-bad-and-you-should-feel-bad.html"

i think simple thing do, can't figure out logically , can't find in twig documentation how handle this. kind of expect twig->render() have parameter fallback page or except array of pages, apparently not.

the answer use try/catch block when rendering page. switched slim routing, didn't make difference on macaw. have this:

try {     $app->render("pages/$slug.html", $data); } catch (exception $e) {     $app->render('pages/404.html', array('error'=> $e)); } 

Comments