/ Published in: PHP
This is a little PHP class that permits to start quickly the creation of a webpage.
When you declare an instance of the class it automatically starts the output buffering and gzip compression, it outputs all the proper tags (doctype, meta, stylesheets, scripts, etc.) until the .
Everything you write after that gets actually placed inside the body, and when the page ends the and tags gets automatically added at the bottom.
In addition to this, the class gets your IP so to display all the possible error/warning messages to you and none to the random users.
EXAMPLES/USAGE: http://claudiobonifazi.com/snippets/Document
When you declare an instance of the class it automatically starts the output buffering and gzip compression, it outputs all the proper tags (doctype, meta, stylesheets, scripts, etc.) until the .
Everything you write after that gets actually placed inside the body, and when the page ends the and tags gets automatically added at the bottom.
In addition to this, the class gets your IP so to display all the possible error/warning messages to you and none to the random users.
EXAMPLES/USAGE: http://claudiobonifazi.com/snippets/Document
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /* -------------------------------------------------------------------- * Document.class.php - PHP class to quick start web page development * Distributed under the Do-wathever-the-hell-you-want-with-it License * * Web site: http://claudiobonifazi.com * Blog: http://claudiobonifazi.com?p=4 * Email: [email protected] * Twitter: @ClaudioBonifazi * ------------------------------------------------------------------- * * Start any webpage this way: * * <?php * include '/path/Document.class.php'; * $html = new Document ( * 'title' => 'The page title tag', * 'description' => 'description tag', * 'keywords' => 'keywords tag', * 'author' => 'author tag', * 'language' => 'en/de/fr/it/sp/whatever', * 'charset' => 'utf-8/whatever', * 'favicon' => 'faviconFileName.ico', * 'stylesheets' => array( 'reset.css'=>'all', 'main.css'=>'all', 'another.css'=>'media' ), * 'ieFallback' => 'theCssFileReservedForOldBrowsers.css', * 'googleFonts' => array('that cool font i saw', 'that other one', 'great Scott!'), * 'scripts' => array( 'jQuery.js', 'head.js', 'my.js', 'whatever.js' ) * ); * ?> * You can leave blank anyone of the parameters, they all have a default value * Anything written after this will be placed inside the <body> tag * The closure tags will be automatically added. * */ class Document{ private $startTime; private $adminIP = '127.0.0.1'; const FAVICONFOLDER = './'; const CSSFOLDER = './components/css/'; const JSFOLDER = './components/js/'; 'title' => 'Untitled Document', 'description' => '', 'keywords' => '', 'author' => '', 'language' => 'en', 'charset' => 'utf-8', 'favicon' => '', 'ieFallback' => '', ); // this can be a solution if you can't set error reporting on php.ini or the .htaccess file // error outputs get printed only to you ( if you properly set the $adminIP property ) $ip = $_SERVER['HTTP_CLIENT_IP']; }else{ $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; else $ip = $_SERVER['REMOTE_ADDR']; } if( $ip == $this->adminIP ){ }else{ } // output buffering via script (if not set by server settings) // storing the script execution start time could be useful //data management $head = $this->head; foreach( $headOverride as $tag=>$val ){ trigger_error("There's no point in adding $tag head property, it won't be considered. Maybe you mispelled it?"); }else{ if( $head[$tag]!=$headOverride ) $head[$tag] = $headOverride[$tag]; } } // starting tags echo "<!doctype html>", "\n<html lang=\"$head[language]\">", "\n\t<head>"; // initial meta tags echo "\n\t\t<title>$head[title]</title>", "\n\t\t<meta name=\"description\" content=\"$head[description]\">", "\n\t\t<meta name=\"keyword\" content=\"$head[keywords]\">", "\n\t\t<meta name=\"author\" content=\"$head[author]\">", "\n\t\t<meta name=\"language\" content=\"$head[language]\">", "\n\t\t<meta name=\"charset\" content=\"$head[charset]\">"; // google web fonts foreach( $head['googleFonts'] as $n=>$name ){ echo "\n\t\t<link rel=\"stylesheet\" href=\"http://fonts.googleapis.com/css?family=".urlencode($name)."\">"; } // stylesheets foreach( $head['stylesheets'] as $file=>$media ) echo "\n\t\t<link rel=\"stylesheet\" href=\"".Document::CSSFOLDER.$file."\" media=\"$media\">"; // Internet Explorer fallbacks if( $head['ieFallback']!='.css' && $head['ieFallback']!='' ) echo "\n\t\t<!--[if lt IE 9]>", "\n\t\t\t<script type=\"text/javascript\" src=\"http://html5shiv.googlecode.com/svn/trunk/html5.js\"></script>", "\n\t\t\t<link rel=\"stylesheet\" href=\"".Document::CSSFOLDER.$head['ieFallback']."\" media=\"screen\">", "\n\t\t<![endif]-->"; // scripts ( I suggest you to use http://headjs.com ) foreach( $head['scripts'] as $n=>$file ) echo "\n\t\t<script src=\"".Document::JSFOLDER.$file.'"></script>'; // favicon echo "\n\t\t<link rel=\"shortcut icon\" href=\"".Document::FAVICONFOLDER.$head['favicon'].'">'; // close and initialize body section echo "\n\t</head>", "\n\t<body>", "\n"; } public function __destruct(){ // when execution ends those ending tags will be automatically added echo "\n\t</body>", "\n</html>"; } } ?>
URL: http://claudiobonifazi.com/snippets/Document