Script 1.1 Base Template

The basic page layout for presenting dynamic code examples with source using HTML5.

Output
BASE TEMPLATE
Source
<?php
require_once('config.php');

//set necessary variables for content
$content = isset($_GET['content'])? htmlspecialchars($_GET['content']): null;
$script = isset($_GET['script'])? htmlspecialchars($_GET['script']):null;
$path = isset($_GET['path'])? htmlspecialchars($_GET['path']):null;
$chapter = isset($_GET['chapter'])? htmlspecialchars($_GET['chapter']): null;
//set default content if none supplied
if (!$content && !$script) $content='home';

//set up path to file for validation
if($script) {
	$url = CHAPTER_PATH.(($chapter)?'/'.$chapter:'').(($path)?'/'.$path:'').(($script)?'/'.$script.'.php':'');
} elseif($content) {
	$url = ($path)? "$path/$content.php":"includes/$content.php";
}
if(!is_file($url) || !is_readable($url)) {
	header('Location: '.SITE_URL);
	exit;
}

//start sessions for scripts that require it
start_session($script);

//check script for any header processing
process_header($script,$chapter,$path);

$meta = get_script_meta($chapter,$script);
$title = get_page_title($content,$script,$chapter);
$subTitle = (is_numeric($script)? 'Script '.$script :(is_numeric($chapter)?'Chapter '.$chapter:$chapter).' - '.ucfirst($script)).' <span class="meta-title">'.$meta['title'].'</span>';

//add the page/script name to the body css class to aid in future styling
$body_class = str_replace(' ','-',(($script)?'script-'.$script:''));
$body_class .= (($script)?' ':'').str_replace(' ','-',(($content)?$content:''));

//begin OUTPUT ?>
<!doctype html>
<html lang="en">  
	<head>
		<?php if(in_array($chapter,array(3,9,10,12,16,17,18, 19))) { ?>
 	 		<link rel="stylesheet"	href="css/style-3.css" type="text/css" media="screen"/>
		<?php } ?>
		<meta charset="utf-8">
		<meta name="description" content="">
		<meta name="viewport" content="width=device-width, initial-scale=1"> 
		<title><?php echo $title;?></title>

		<link rel="icon" type="image/png" href="favicon.png" />
		<link rel="stylesheet" type="text/css" href="css/style.css">

		
		<?php 
		// load jQuery ui stylesheet for effects
		if($script=='16.5') {
		echo '<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">';
		}

		//load jQuery only if needed
		if(in_array($chapter,array(15)) || in_array($script,array(16.5))) {
			echo '<script type="text/javascript" charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>';
		} ?>

		<?php echo ($script=='15.3')?'<script type="text/javascript" src="js/15.2.js" charset="utf-8"></script>':'';?>
		<?php echo ($script=='15.4')?'<script type="text/javascript" src="js/15.5.js" charset="utf-8"></script>':'';?>
		<?php echo ($script=='15.6')?'<script type="text/javascript" src="js/15.7.js" charset="utf-8"></script>':'';?>
		<?php echo ($script=='15.8')?'<script type="text/javascript" src="js/15.10.js" charset="utf-8"></script>':'';?>
		<?php echo ($script=='16.5')?'<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" charset="utf-8"></script>
		<script>
		$(function() {
		$( ".datepicker" ).datepicker({
		 	changeMonth: true,
			changeYear: true,
			yearRange: "-100:+100"
			});
		});
		</script>':'';?>

		<script src="js/general.js"></script>
		<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
	  	<!--[if lt IE 9]>
	  	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	  	<![endif]-->
	</head>
	<body class="<?php echo $body_class; ?>">
		<?php include('includes/nav.php'); ?>
		<?php include('includes/header.php'); ?>
		<div class="main-content">
			<?php if($script) { ?>
					<header>
						<h1><?php echo $subTitle; ?></h1>
						<?php if($meta['description']!='') { ?>
							<p class="meta-description"><?php echo $meta['description'];?></p>
						<?php } ?>
					</header>
					<div class="exercise-container">
						<span class="content-caption">Output</span>
						<section class="exercise">
							<?php
								ob_start();
								include($url); 
								$output = ob_get_contents();
								ob_end_clean();
								$output = preg_replace(array("/<!doctype>/","/<html>/","/<\/html>/","/<body>/","/<\/body>/","/<head>(?s)(.*)<\/head>/"), '', $output);
								echo $output;
							?>
						</section>
					</div>
					<?php 
						if(preg_match('/\b1.1\b/', $script)){
							$file = file_get_contents(basename($_SERVER['PHP_SELF']), FILE_USE_INCLUDE_PATH);
						} else {
							$file = file_get_contents($url);
						}
						echo "<div class='source-container'><span class='content-caption'>Source</span><section class='source'><pre class='prettyprint linenums'>".htmlspecialchars($file)."</pre></section></div>";
			} elseif($content) {
				include($url);
			} ?>
		</div>
		<?php include('includes/footer.php'); ?>
	</body>  
</html>