Skip to content

Instantly share code, notes, and snippets.

@psaia
Created January 19, 2012 18:30
Show Gist options
  • Save psaia/1641727 to your computer and use it in GitHub Desktop.
Save psaia/1641727 to your computer and use it in GitHub Desktop.
A super simple View engine to drive static flatfile websites. Good for when MVC is overkill.
<?php // /application/views/ ?>
<?php
$title = '404 Error';
//$description = '';
?>
Page not found.
<?php // /application/ ?>
<?php
/*
* Peach
* For super basic websites with no need for controllers,
* models or a database.
*
* Author: Pete Saia
For URL's without having index.php prepended place this inside
the .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
*/
/*
* Constants.
*/
// Directory where views are stored. No leading or trailing slash.
define('VIEWS_DIR', 'views');
// Directory where libs are stored. No leading or trailing slash.
define('LIBS_DIR', 'libs');
// Directory where data flat files are stored.
define('DATA_DIR', 'data');
// Landing page view.
define('HOME_VIEW', 'home');
// View name for 404 page.
define('NOT_FOUND_VIEW', '404');
// View file extension.
define('EXT', '.php');
// The absolute path to the application directory.
define('SYS_ROOT', dirname(__FILE__));
/*
* Includes.
*/
require_once(LIBS_DIR.'/view.php');
require_once(LIBS_DIR.'/helpers.php');
/*
* Initialize view.
*/
$view = new View;
?>
<?php // /application/libs/ ?>
<?php
/* Helper Methods
*
* These are site specific helper methods and are NOT
* required by Peach.
*
*/
class Helpers
{
/*
* Truncate by words.
*
* @return String
*/
public static function truncate_words($str, $limit = 100, $end_char = '&#8230;') {
if (trim($str) == '') {
return strip_tags($str);
}
preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
if (strlen($str) == strlen($matches[0])) {
$end_char = '';
}
return strip_tags(rtrim($matches[0]).$end_char);
}
/*
* Returns current URL segments.
*
* @return String
* URI: /this/is//a/path/to///nothing
* echo segments( 4 ); // path
* echo segments( 18 ); // NULL
* echo segments(); // /this/is/a/path/to/nothing
*/
public static function segments($index = NULL)
{
static $segments;
// build $segments on first function call
if (NULL === $segments)
{
$segments = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode( '/', $segments);
$segments = array_filter($segments);
$segments = array_values($segments);
}
// if $index is NULL, emulate REQUEST_URI
if (NULL === $index)
{
return '/' . implode('/', $segments);
}
// return the segment index if valid, otherwise NULL
$index = (int) $index - 1;
return isset($segments[$index]) ? $segments[$index] : NULL;
}
}
?>
<?php require_once('application/bootstrap.php'); ?><!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie<?php echo ($view->is_front) ? ' home' : ' inner'; ?>" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie<?php echo ($view->is_front) ? ' home' : ' inner'; ?>" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie<?php echo ($view->is_front) ? ' home' : ' inner'; ?>" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js<?php echo ($view->is_front) ? ' home' : ' inner'; ?>" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title><?php echo $view->title ?></title>
<meta name="description" content="<?php echo $view->description ?>">
<meta name="author" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/assets/css/style.css">
<script src="/assets/js/modernizr.js"></script>
</head>
<body>
<?php echo $view->content ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/assets/js/map/libs/underscore-min.js"></script>
<script src="/assets/js/map/libs/backbone-min.js"></script>
<?php endif; ?>
<!--[if lt IE 7 ]>
<script src="//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js"></script>
<script>window.attachEvent('onload',function(){CFInstall.check({mode:'overlay'})})</script>
<![endif]-->
</body>
</html>
<?php // /application/libs/ ?>
<?php
/*
* This class is resonsible for retrieving the correct view
* based on the current URL.
* Author: Pete Saia
*/
class View
{
// The actual view itself. aka body.
public $content = '';
// The tite of the page. (set in view)
public $title = '';
// The description of the page. (set in view)
public $description = '';
// Boolean var to detect if homepage or not.
public $is_front = false;
/*
* Initialize and set vars.
*
* @return void
*/
public function __construct()
{
// Get requested file path attempting to open.
$file = $this->_get_route_path();
if ($file) // If there is a path.
{
$request_path = SYS_ROOT.'/'.VIEWS_DIR.'/'.$file.EXT;
}
else // If not use home page view.
{
$this->is_front = true;
$request_path = SYS_ROOT.'/'.VIEWS_DIR.'/'.HOME_VIEW.EXT;
}
// Make sure the requested view exists. If it doesn't
// set the 404.php view.
if ( ! is_file($request_path))
{
$request_path = SYS_ROOT.'/'.VIEWS_DIR.'/'.NOT_FOUND_VIEW.EXT;
}
// Load the view and set the variables.
$this->_set_vars($request_path);
}
/*
* Form a path to the requested file from the URI. Strip
* out index.php as well.
*
* @return String or false if there is no URI.
* @sample profiles/pete
*/
private function _get_route_path ()
{
// The path.
$path = false;
// Parse the URI and form array of segments.
$segments = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode( '/', $segments);
$segments = array_filter($segments);
$segments = array_values($segments);
// If there are segments.
if ( ! empty($segments))
{
// Loop through each segment to form the string.
foreach ($segments as $seg)
{
// Do not include index.php if links are not pretty.
if ($seg !== 'index.php')
{
// Build link.
$path .= $seg.'/';
}
}
// Strip off last slash because .php will later be appended.
$path = preg_replace('/\/$/', '', $path);
}
return $path;
}
/*
* Using the full path to the file this loads the view and
* sets the variables.
*
* @return void
*/
private function _set_vars ($path)
{
ob_start();
include $path;
$this->content = ob_get_contents();
$this->title = $title;
$this->description = $description;
ob_end_clean();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment