Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. // Load up composer autoload, and instantiate the application.
  2. require 'vendor/autoload.php';
  3.  
  4. $app = new SlimSlim;
  5.  
  6. // Register a singleton of the Mustache engine, and tell it to cache
  7. $app->container->singleton('mustache', function () {
  8. return new Mustache_Engine(array(
  9. 'cache' => 'storage',
  10. 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/pages', array('extension' => '.html'))
  11. ));
  12. });
  13.  
  14. // Helper function to render templates
  15. function renderTemplate($name, $data = array()) {
  16. global $app;
  17.  
  18. if (file_exists('config.php')) {
  19. $data = (require 'config.php');
  20. }
  21.  
  22. $data += array(
  23. 'resourceUri' => $app->request->getResourceUri() ?: 'index',
  24. 'request' => $app->request
  25. );
  26.  
  27. return $app->mustache->loadTemplate($name)->render($data);
  28. }
  29.  
  30. // Loads a page by the given name/path
  31. function loadPage($path) {
  32. global $app;
  33.  
  34. // Set up the base path
  35. $f = 'pages/' . $path;
  36.  
  37. if (file_exists($f . '.html')) {
  38. // If there's an HTML file, render the mustache template
  39. return renderTemplate($path . '.html');
  40. } elseif (file_exists($f . '.php')) {
  41. // If there's a PHP file, return it
  42. return (require $f . '.php');
  43. } elseif ($path != '404') {
  44. // Otherwise, go get the 404 page
  45. return loadPage('404');
  46. } else {
  47. // But if the user doesn't have a 404 page made, return a plain 404
  48. $app->halt(404);
  49. }
  50. }
  51.  
  52. // Index page
  53. $app->get('/', function () use ($app) {
  54. echo loadPage('index');
  55. });
  56.  
  57.  
  58. // Route to everything else
  59. $app->get('/.*?', function () use ($app) {
  60. // Get the current request path
  61. $path = $app->request->getResourceUri();
  62.  
  63. // Make sure the user isn't trying to escape and do nasty things
  64. if (!preg_match('/^[A-z/-_]+$/', $path)) {
  65. echo loadPage('404');
  66. }
  67.  
  68. // Send the page off to get loaded
  69. echo loadPage($path);
  70. });
  71. $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement